MySql连接两个表和左表的错误结果
我有一个两个表,我离开了join,想要两个表的值之和。MySql连接两个表和左表的错误结果,mysql,sql,Mysql,Sql,我有一个两个表,我离开了join,想要两个表的值之和。 但未成功检索第二个表的结果(SUM未正确分组) 表test1 id redeem_count cost camp_id 1 2 500 1 2 3 1000 1 3 2 2000 2 4 2 3000 2 5 2
但未成功检索第二个表的结果(SUM未正确分组)
表test1
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
表test2
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
输出
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
camp_id redeem cost bill earning
1 5 1500 15000 165
2 4 5000 NULL NULL
3 2 1200 NULL NULL
camp_id redeem cost bill earning
1 5 1500 4000 50
2 4 5000 11000 115
3 2 1200 0 0
期望的结果
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
camp_id redeem cost bill earning
1 5 1500 15000 165
2 4 5000 NULL NULL
3 2 1200 NULL NULL
camp_id redeem cost bill earning
1 5 1500 4000 50
2 4 5000 11000 115
3 2 1200 0 0
我已经执行了以下SQL
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM (SELECT COALESCE(SUM(redeem_count),0) AS redeem, COALESCE(SUM(cost),0) AS cost, id,camp_id FROM test1 GROUP BY camp_id) t1
LEFT JOIN(SELECT COALESCE(SUM(bill_amount),0) AS bill, COALESCE(SUM(earning),0) AS earning, test1_id FROM test2) t2
ON t1.id = t2.test1_id
我如何才能达到预期的结果?请您提供解决方案好吗 试试这个:
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM (SELECT COALESCE(SUM(redeem_count),0) AS redeem, COALESCE(SUM(cost),0) AS cost, id,camp_id FROM test1 GROUP BY camp_id) t1
LEFT JOIN(SELECT COALESCE(bill_amount) AS bill, COALESCE(earning) AS earning, test1_id FROM test2) t2
ON t1.id = t2.test1_id
更新:1
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM
(
SELECT
COALESCE(SUM(redeem_count),0) AS redeem,
COALESCE(SUM(cost),0) AS cost,
id,
camp_id
FROM test1
GROUP BY camp_id) t1
LEFT JOIN(
SELECT
SUM(bill_amount) AS bill,
SUM(earning) AS earning, test1_id FROM test2 GROUP BY test1_id) t2
ON t1.camp_id= t2.test1_id
我的代码生成了如下输出:
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
camp_id redeem cost bill earning
1 5 1500 4000 50
2 4 5000 11000 115
3 2 1200 (NULL) (NULL)
已编辑
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
创建子查询,就像只查询所需结果一样,然后连接这些结果
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
(未测试查询!)
id redeem_count cost camp_id
1 2 500 1
2 3 1000 1
3 2 2000 2
4 2 3000 2
5 2 1200 3
id bill_amount earning test1_id
1 4000 50 1
2 5000 55 3
3 6000 60 4
您是否正在处理@jmail-answer的粘贴(同一问题)请参阅评论检查我的
更新:1
回答和输出,问题是如果您不提及test1\u id=3的账单金额和收入的值,那么只有它产生空值
。是的,我不想按test1\u id添加组…这会有问题,先生。。。意味着test1\u id只是外键…请现在查看表2(test2)(问题更新)。我不想在左边的桌子上分组……是的,我看到了。事实上,这不是一个“想要还是不想要”的问题。你问了如何做,想要的输出需要GROUPBY子句。请看test2表…我有值changedOK,我现在明白了,对不起。编辑了我的回答,并将昏迷添加到SUM(T2.bill)的末尾作为bill