I'm trying to optimize this query:
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'passenger_group_id'
AND meta_value in (SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'group_event' AND meta_value = '14608')
AND post_id IN (SELECT post_id
FROM wp_postmeta
WHERE meta_value='Cancelled')
AND post_id NOT IN (SELECT ID
FROM wp_posts
WHERE post_status='trash')
Any help would be greatly appreciated.
you can use EXISTS
clause alone and add NOT IN condition can be made NOT EXISTS
select wp1.post_id
from wp_postmeta wp1
WHERE meta_key = 'passenger_group_id'
and exists ( select 1 FROM
wp_postmeta wp2
where ( (wp2.meta_key = 'group_event' AND wp2.meta_value = '14608' ) or wp2.meta_value ='Cancelled' )
and wp2.post_id = wp1.meta_value
)
and not exists ( select 1 from
wp_posts p
on p.id = wp1.post_id
and p.post_status = 'trash')
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.