Left join does not work with Count() as expected

2009-02-19 Thread Artem Kuchin
I have two simple tables. One - list of forums, second - list of messages and i want to get the list of forums with number of messages in each. Here is the query: SELECT forums.id , COUNT( forum_msg.id ) AS cnt FROM forums LEFT JOIN forum_msg ON forums.id = forum_msg.forum_id ORDER BY

Re: Left join does not work with Count() as expected

2009-02-19 Thread Olaf Stein
You need to group by forum_id... On 2/19/09 11:09 AM, Artem Kuchin mat...@itlegion.ru wrote: I have two simple tables. One - list of forums, second - list of messages and i want to get the list of forums with number of messages in each. Here is the query: SELECT forums.id , COUNT(

Re: Left join does not work with Count() as expected

2009-02-19 Thread Peter Brawley
SELECT forums.id , COUNT( forum_msg.id ) AS cnt FROM forums LEFT JOIN forum_msg ON forums.id = forum_msg.forum_id ORDER BY forums.sorder ASC Missing GROUP BY. PB - Artem Kuchin wrote: I have two simple tables. One - list of forums, second - list of messages and i want to get the list of

RE: Left join does not work with Count() as expected

2009-02-19 Thread Martin Gainty
Following the documentation available at http://dev.mysql.com/doc/refman/5.0/en/join.html t1t2 a b a c --- --- 1 x 2 z 2 y 3 w Then a natural left join would product these results mysql SELECT * FROM t1 NATURAL LEFT JOIN t2; +--+--+--+ | a| b