select 10 rows joined with all rows from other table

50 views Asked by At

I have 2 tables post and comments and I want to select 10 posts joined with all comments:

 SELECT * from post p left join comments c on p.id = c.post LIMIT 10;

this query will not work since the limit is for post with comments and not for the posts alone. How can I do this then? Is it possible without using inner query?

2

There are 2 answers

0
Vignesh Kumar A On

Try this

SELECT P.*,C.* 
From (SELECT * FROM post LIMIT 0,10) AS P
             Left Join comments C ON C.Id = P.ID
2
Sirko On

The straight forward solution would be to separate a little:

SELECT * 
FROM comments c 
LEFT JOIN (SELECT * FROM post LIMIT 10) p
  ON p.id = c.post;