【问题标题】:Subquery returns more than 1 row with a Wordpress postmeta query子查询使用 Wordpress postmeta 查询返回超过 1 行
【发布时间】:2021-01-18 19:16:30
【问题描述】:

我正在尝试从子网站的帖子和 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"

我收到“子查询返回超过 1 行”错误。我不明白为什么会这样。如果我将 WHERE 子句中的 xyz_8_posts.ID 替换为实际 ID,则查询将返回一个标题。

【问题讨论】:

  • 首先,内部连接似乎是多余的,另一件事是,可能有一些帖子在元表中为元键presentation_title有多个条目

标签: mysql sql wordpress post-meta


【解决方案1】:

你有一个额外的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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多