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.

Read More

Sincere thanks for any help. It is greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. 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