I use Wordpress/Woocommerce with MySQL.
I need to find all orders with the same billing phone as a given one.
The basic SQL is below. The problem is that when the phone numbers were stored in the DB they were not canonized and so there are all possible forms like `0YZ-abcd`, `0YZabcd`,`+KMYZabcs`, etc
I already have a PHP function to canonize any phone number. My question is how to apply that function to every row's *\_billing\_phone* column which is being considered in the WHERE clause.

The original SQL is

```
 $canonized_phone_to_compare_to = canonize_phone_number($original_phone_number);

 $results = $wpdb->get_col( "
    SELECT p.ID FROM {$wpdb->prefix}posts AS p
    INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
    WHERE pm.meta_key = '_billing_phone' 
    AND pm.meta_value = '" . $canonized_phone_to_compare_to . "'      
" );

```

What I need is to find a way to apply `canonize_phone_number()` to `pm.meta_value` just before it gets compared. In other words, each time the SQL 'checks' if the pm.meta\_value equals to $canonized\_phone\_to\_compare\_to I want the SQL to apply my function (or a stored procedure maybe?) to pm.meta\_value and re-format that field

Or any other way to tackle this?