使用 Wordpress postmeta 查询子查询 returns 多于 1 行

Subquery returns more than 1 row with a Wordpress postmeta query

我正在尝试从子站点的帖子和 postmeta 表中提取数据:

SELECT xyz_8_posts.ID,

(select xyz_8_postmeta.meta_value
from xyz_8_postmeta
inner join xyz_8_posts
on xyz_8_postmeta.post_id = xyz_8_posts.ID
where xyz_8_postmeta.meta_key = 'presentation_title'
AND xyz_8_postmeta.post_id = xyz_8_posts.ID
) as 'Presentation Title'


FROM xyz_8_posts
WHERE xyz_8_posts.post_type = "presenters"

我收到“子查询 returns 多于 1 行”错误。我不明白这是为什么。如果我用实际 ID 替换 WHERE 子句中的 xyz_8_posts.ID 查询 returns 一个标题。

你有一个额外的JOIN,你只需要一个相关的子查询:

SELECT p.ID,
      (select pm.meta_value
       from xyz_8_postmeta pm
       where pm.post_id = p.ID and
             pm.meta_key = 'presentation_title'
      ) as Presentation_Title
FROM xyz_8_posts p
WHERE p.post_type = 'presenters'