简体   繁体   中英

Return all joined rows in SQL

So I am messing around with wordpress and trying to get all the rows a row joins to. For now, I can only seem to get one row it joins to. I know there has to be some element of GROUP BY, but I just can't seem to figure it out.

SELECT posts.*, meta.meta_value
                FROM wp_g6exrf_posts as posts
                LEFT OUTER JOIN wp_g6exrf_postmeta as meta
                ON meta.post_id = posts.ID
                WHERE ! ( posts.post_status = 'auto-draft' ) 
                GROUP BY meta.post_id

So I am joining to the meta table and expecting more than one column to meet the join condition.

Sincere thanks for any help. It is greatly appreciated.

postmeta makes very little sense without seeing meta_key .

GROUP BY doesn't make sense unless you're doing some kind of aggregate. It's crushing your multiple rows for each post into one, and returning just one of your meta_value items, a randomly chosen one.

Try this:

SELECT posts.*, 
       meta.meta_key, meta.meta_value
  FROM wp_g6exrf_posts as posts
  LEFT OUTER JOIN wp_g6exrf_postmeta as meta ON meta.post_id = posts.ID
 WHERE ! ( posts.post_status = 'auto-draft' ) 
 ORDER BY posts.id, meta.meta_key, meta.meta_value

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM