Question

I have 3 tables: WAS, USER and connection table WAS_USER. The thing I would like to achieve is to get all the rows from table WAS and if there are many users to the same WAS I would like to have one row for each one. for example:

WAS:
    id | name
    1  | 'was1'
    2  | 'was2'

USER:
    id | name
    1  | 'user1'
    2  | 'user2'

WAS_USER:
    userId | wasId
    1      | 1
    2      | 1

So after queering I need to get this:

wasrId | userId | wasName | userName
1      | 1      | 'was1'  | 'user1'
1      | 2      | 'was1'  | 'user2'
1      | 2      | 'was1'  | 'user2'

Ordinary join between the 3 tables will give my only rows from WAS, what I need is some kind of left join on 3 tables.

Was it helpful?

Solution

Ok, here is what worked for me:

SELECT * 
FROM 
(SELECT * FROM WAS W LEFT JOIN WAS_USER WU ON W.id=WU.wasID) TMP LEFT JOIN USER U ON TMP.userId=U.userId

Don't know about the efficiency though...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top