I am trying to select the ID from a wp_posts table where I need to do 3 conditions.
-
Is the ID matching with the post_id from the wp_postmeta table where the meta_key = ‘gtp_analytics_client_id’ and the meta_value is not empty.
-
Is the ID matching with the post_id from the wp_postmeta table where the meta_key = ‘gtp_conversion_uploaded’ and the meta_value is not equal to 1.
- Is the ID matching with the post_id from the wp_postmeta table where the meta_key = ‘gtp_lead_revenue’ and the meta_value is not empty.
I am a beginner with SQL. This is what I have now, but I cannot use multiple IN’s. So I think I need to do it another way.
SELECT ID
FROM wp_posts
WHERE ID IN (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'gtp_analytics_client_id' AND meta_value != '')
AND IN (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'gtp_conversion_uploaded' AND meta_value != 1)
AND IN (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'gtp_revenue' AND meta_value != '')
I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘IN (SELECT post_id FROM wp_postmeta WHERE meta_key = ‘gtp_conversion_uploaded’ A’ at line 4
The
and
is not part of thein
operator, it is three separatein
operators, so you need the first operand (ID
) for all of them:You could also write that as three joins:
When it can be either of the 3 cases