2017-04-27 53 views
-2
 
I have below three tables in database: 

users:
id emailaddress
1 [email protected]
2 [email protected]
3 [email protected]
allowance_details
id user_id allowance date
1 1 13000 15/02/2017
2 1 10000 23/01/2016
3 2 25000 15/02/2017
4 3 15000 10/1/2017
5 3 7000 12/12/2015
bonus_details
id user_id bonus date
1 1 7000 15/02/2017
2 1 5000 17/01/2016
3 1 3500 23/07/2015
4 2 4500 10/1/2017
5 2 6000 16/03/2016
6 3 2500 18/6/2016
7 3 3800 24/09/2015
I want to combine above three tables and want below result: id emailaddress allowance date bonus date
1 [email protected] 13000 15/02/2017 7000 15/02/2017
1 [email protected] 10000 23/01/2016 5000 17/01/2016
1 [email protected] 3500 23/07/2015
2 [email protected] 25000 15/02/2017 4500 10/1/2017
2 [email protected] 6000 16/03/2016
3 [email protected] 15000 10/1/2017 2500 18/6/2016
3 [email protected] 7000 12/12/2015 3800 24/09/2015
3 [email protected] 4000 12/7/2014
Any one help me out what will be the correct MySQL query to fetch such result? Below is my query: SELECT DISTINCT u.id, u.emailaddress, bon.bonus, bon.date,alw.allowance,alw.date FROM users u LEFT JOIN bonus_details bon ON bon.user_id = u.id LEFT JOIN allowance_details alw ON alw.user_id = u.id
+0

哪裏是你的代碼/查詢嘗試? –

+0

並請提出具體問題。 – Matt

+0

我已經添加了我用過的查詢。 – Himani

回答

0

請嘗試以下操作:

選擇u.id,u.emailaddress,aw.allowance,aw.date,bd.bonus,從bd.date用戶ü

內部聯接allowance_Details AW上aw.user_id = u.id

內部聯接bonus_details BD上bd.user_id = u.id

+0

我已經嘗試過,但沒有給出我想要的結果。 – Himani

+0

你可以告訴你在運行後得到了什麼結果。你可以使用Left join而不是inner,或者嘗試左外連接 – honey