您的查询 1) 甚至不应该在 where 子句中给定 id 的语法不明确 2) 连接表是没有意义的,因为您在选择中没有使用它的任何内容 3) 根据您在问题中提供的少量信息在 receive_bardana 和 receive_wheat 之间存在一对多的关系,这意味着总和是在所有连接的行上,例如
create table receive_bardana(id int,bales int);
create table receive_wheat(id int);
insert into receive_bardana values (1,10),(2,20);
insert into receive_wheat values(1),(1),(2),(2),(2);
select *
from receive_bardana
join receive_wheat
on receive_bardana.id = receive_wheat.id
where receive_bardana.id;
结果
+------+-------+------+
| id | bales | id |
+------+-------+------+
| 1 | 10 | 1 |
| 1 | 10 | 1 |
| 2 | 20 | 2 |
| 2 | 20 | 2 |
| 2 | 20 | 2 |
+------+-------+------+
5 rows in set (0.00 sec)
以及您的(固定)查询
select sum(bales) as bales
from receive_bardana
join receive_wheat
on receive_bardana.id = receive_wheat.id
where receive_bardana.id;
总和正确返回
+-------+
| bales |
+-------+
| 80 |
+-------+
1 row in set (0.00 sec)
这完全解释了您遇到的“问题”。
如果您想知道您正在尝试做什么,我建议您提出一个新问题来描述您正在尝试做什么,而不是仅仅说这段代码没有按照我的预期执行(实际上它符合我的预期并且是没有'错')