MySQL Forums
Forum List  »  General

Re: SQL Question
Posted by: Felix Geerinckx
Date: May 10, 2005 06:03AM

Rob de Jong wrote:

> I have two tables, table 1 with (id, name) and table 2 with (id, userid, ranking). The id's are the
> same, primary key table 1 is (id), and primary key table 2 is (id, userid).
>
> Now I want to have a list of all entries in table 1, and - if available for a user - ordered by
> ranking. The problem is that not all entries in table 1 will have a ranking in table 2, yet I do
> want those in my result.

> A join on id's however gives
> aaa, 10
> bbb, 20
> so drops the ccc-row

You need a LEFT JOIN:

SELECT
table1.name,
table2.ranking
FROM table1
LEFT JOIN table2 ON table2.id = table1.id

See http://dev.mysql.com/doc/mysql/en/join.html

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
May 10, 2005 05:42AM
Re: SQL Question
May 10, 2005 06:03AM
May 10, 2005 07:14AM
May 10, 2005 07:51AM
May 10, 2005 10:13AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.