0

I have 3 tables:

First "placement" enter image description here

Second "user_info"

enter image description here

Third "user_placements"

enter image description here

I want to get all placement data with user infos,

enter image description here

How to do it?

I tried this, but result it not what I expected:

SELECT *, user_placements.id AS user_placements_id, placement.id AS placement_id
FROM placement
LEFT OUTER JOIN user_placements ON placement.id = user_placements.id_placement
jarlh
  • 42,561
  • 8
  • 45
  • 63
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • Just list those fields between the select and the from that you want to include in the resultset. And also join all 3 tables, not just 2. – Shadow Jan 31 '17 at 09:29
  • there is any relation between user_placements and user_info or placement and user_info ? – krishn Patel Jan 31 '17 at 09:29
  • @krishnpatel `user_info.id = user_placements.id_user` and `placement.id = user_placements.id_placement` – NickUnuchek Jan 31 '17 at 09:31

2 Answers2

1

you need to one more join with user info

SELECT placement.*,user_info.id as user_info_id,user_info.name as user_name,user_info.mobile as user_mobile
 FROM placement LEFT OUTER JOIN user_placements ON placement.id = user_placements.id_placement
    LEFT OUTER JOIN user_info ON user_info.id = user_placements.id_user
krishn Patel
  • 2,579
  • 1
  • 19
  • 30
1

You are missing the second join:

SELECT *
FROM placement AS p
JOIN user_placements AS up ON p.id = up.id_placement
JOIN user_info AS u ON up.id_user = u.id

Replace the wildcard with the data you want. You will of course get duplicated data with this query.

RyanS
  • 78
  • 1
  • 10