1

I am trying to pull data from a subsite's posts and postmeta tables:

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"

I'm getting a "Subquery returns more than 1 row" error. I don't understand why this is. If I replace xyz_8_posts.ID in the WHERE clause with an actual ID the query returns one title.

Anthony
  • 722
  • 1
  • 9
  • 25
  • 1
    Firstly the inner join seems redundant and another thing is that there can be some posts who have multiple entries in meta table for meta key presentation_title – M Khalid Junaid Jan 18 '21 at 19:20

1 Answers1

2

You have an extra JOIN where you just want a correlated subquery:

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'
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786