I am stuck with this problem:
I made 3 custom meta_key
in wp_postmeta
. Now I have to select all the records that contain in the column meta_value
, one or more words, from a string I provide.
This is the SQL I wrote, but it’s not giving me back any results, even if I have one word matching the REGEX.
SELECT post_id FROM wp_postmeta WHERE
meta_key = 'packaging_attributes'
AND
meta_key = 'properties_attributes'
AND
meta_key = 'tech_functions_attributes'
AND
meta_value REGEXP 'chased-floor|chased-wall-plasterboard|alarm-en|0-22|0-220-75|15|25'
This is the screenshot from the db:
As you can see in row 1008 I have chased-wall-plasterboard, that should match the above select, and give me back 39 as result. But it’s not working.
If I remove the three meta_key = column_name
I am going to get tons of results but not the 39.
2
Answers
I assume that what you really want are meta posts having records with all 3 meta values in the current
WHERE
clause. If so, then aggregation is one approach:It is not clear whether the restriction on
meta_value
really belongs there.You should use OR operation to gather all 4 rows.