2017-04-27 50 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