I have this sql query that is not doing its job with "NOT LIKE". The strangest thing is that if I remove the "NOT" and leave the LIKE if it works normally doing its job of listing everything that the "LIKE" indicates.
But I want the opposite I need it to omit everything from the "NOT LIKE" and I get the opposite rather list all that. Apparently "NOT LIKE" is doing the opposite.
Why does this happen?
This would be my SQL query
This code is original and works great.
SELECT last_update.post_id
FROM wp_postmeta last_update
JOIN wp_posts AS post ON last_update.post_id = post.ID
WHERE 1659698743 - last_update.meta_value > 86400
AND last_update.meta_key = '_ety_last_update'
AND post.post_status = 'publish'
ORDER BY last_update.meta_value ASC LIMIT 17
I added this so that it would omit the "NOT LIKE"
SELECT last_update.post_id
FROM wp_postmeta last_update
JOIN wp_posts AS post ON last_update.post_id = post.ID
JOIN wp_postmeta AS _ety_product_info ON last_update.post_id = _ety_product_info.post_id
WHERE 1659698743 - last_update.meta_value > 86400
AND last_update.meta_key = '_ety_last_update'
AND _ety_product_info.meta_value NOT LIKE '%Product data not found%'
AND post.post_status = 'publish'
ORDER BY last_update.meta_value ASC LIMIT 17
Just add these 2 lines but they don't do their job.
JOIN wp_postmeta AS _ety_product_info ON last_update.post_id = _ety_product_info.post_id
AND _ety_product_info.meta_value NOT LIKE '%Product data not found%'
What am I doing wrong?
My goal is to skip everything that contains "Product data not found" inside the "meta_value" - "_ety_product_info" and is not displayed in the query
CodePudding user response:
You don't need another table, a simple EXISTS
which will check if any postid has such a meatvlaue
SELECT last_update.post_id
FROM wp_postmeta last_update
JOIN wp_posts AS post ON last_update.post_id = post.ID
WHERE 1659698743 - last_update.meta_value > 86400
AND last_update.meta_key = '_ety_last_update'
AND NOT EXiSTS ( SELECT 1 FROM wp_postmeta WHERE meta_value LIKE '%Product data not found%' AND last_update.post_id = post_id)
AND post.post_status = 'publish'
ORDER BY last_update.meta_value ASC LIMIT 17