如何从链接到主查询结果的子查询返回 MySQL 中的字段?
我在 WordPress 数据库上使用的查询遇到了一些问题。下面的查询返回 meta_value
的正确且预期的数据,但我的问题来自于我没有从 中获取原始 post_id
返回的字段子查询,所以我无法将特定的 meta_value
与原始 post_id
链接起来 - 我可能很需要重新构造它,但我有点迷失关于如何返回与它找到的meta_value关联的数据。
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'
示例数据
post_id meta_key meta_value
1 _thumbnail_id 2
2 _wp_attached_file image.jpg
例如,给定 1 个或多个 post_id ('1') 的列表,我找到 meta_value ('2') 并查找具有匹配 post_id ('2') 和指定 meta_key ('_wp_attached_file) 的另一个条目'),我需要返回meta_value('image.jpeg')和原始 post_id('1')
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用自连接:
Just use a self join: