gpt4 book ai didi

mysql - 添加两个独立表的SUM

转载 作者:行者123 更新时间:2023-11-29 00:29:45 25 4
gpt4 key购买 nike

我有两张 table

td_sell

|----------|----------------|------------------|
| id | user_id | price |
|----------------------------------------------|
| 1 | 2 | 10 |
|----------------------------------------------|
| 2 | 1 | 5 |
|----------------------------------------------|
| 3 | 2 | 3 |
|----------------------------------------------|

和 td_commsion

|----------|----------------|------------------|
| id | user_id | price |
|----------------------------------------------|
| 1 | 1 | 3 |
|----------------------------------------------|
| 2 | 1 | 5 |
|----------------------------------------------|
| 3 | 2 | 3 |
|----------------------------------------------|

现在我想要一个像这样的sql查询

SELECT (SUM(td_sell.price) + SUM(td_comission.price)) AS his_earning
FROM td_sell, td_comission
WHERE td_sell.user_id='1'
AND td_comission.user_id='1'

但显示异常结果

结果应该是 13,但显示的是 29

最佳答案

这会起作用:

SELECT (SELECT SUM(s.price) FROM td_sell s WHERE s.user_id = 1)
+
(SELECT SUM(c.price) FROM td_comission c WHERE c.user_id = 1)

演示:SqlFiddle

关于mysql - 添加两个独立表的SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com