I'm having a bit of a problem with a query I'm using on a wordpress database. The query below is returning the correct and expected data for meta_value
but my problem comes from the fact I don't get a field returned for the original post_id
from the subquery , so I'm not able to link a specific meta_value
with the original post_id
- I may well need to restructure this but I'm a bit lost as to how to return this data associated with the meta_value it found.
SELECT meta_value
FROM wp_postmeta
WHERE post_id IN (SELECT meta_value FROM wp_postmeta WHERE post_id IN ('1','2','3','4'))
AND meta_key = '_wp_attached_file'
Sample data
post_id meta_key meta_value
1 _thumbnail_id 2
2 _wp_attached_file image.jpg
So as an example, given a list of 1 or more post_ids ('1'), I find the meta_value ('2') and look for another entry with a matching post_id ('2') and specified meta_key ('_wp_attached_file'), and I need to return both the meta_value ('image.jpeg') and the original post_id ('1')
Thanks in advance
Just use a self join:
select wp1.*, wp2.*
from wp_postmeta wp1, wp_postmeta wp2
where wp2.post_id in (1,2,3)
and wp1.meta_value = wp2.post_id and wp2.meta_key = '_wp_attached_file';
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.