Mysql错误:子查询返回超过1行
嗨,我有一个mysql查询来获取一些id(studentID),如下所示Mysql错误:子查询返回超过1行,mysql,Mysql,嗨,我有一个mysql查询来获取一些id(studentID),如下所示 select t1.studentID from remittedfees t1 where (select t2.fees from feesasigned t2 where t2.studentID=t1.studentID) = (select sum(t3.remittedAmt) from
select t1.studentID
from remittedfees t1
where (select t2.fees
from feesasigned t2
where t2.studentID=t1.studentID) = (select sum(t3.remittedAmt)
from remittedfees t3
where t3.studentID = t1.studentID);
但是查询返回以下错误
ERROR 1242 (21000): Subquery returns more than 1 row
如何重写查询以获得结果?很可能是
select t2。fees
查询正在执行错误所述的操作-返回多行。当进行这样的相等比较时,=
的两边必须是单个值。如果其中一方返回2+值,那么您将得到
1 = 1
2
3
好的。。。什么是平等的?1=1? 2=1? 3=1? 比较结果应使用哪一个“真值”值?我想您应该检索
学生ID
,其费用表中的费用与汇款费用表中的总汇款金额相同。然后试试这个:
SELECT a.studentID, b.fees, SUM(a.remittedAmt) TotalFees
FROM remittedfees a INNER JOIN feesasigned b
on a.studentID = b.studentID
GROUP BY a.studentID, b.fees
HAVING b.fees = SUM(a.remittedAmt)
尝试此[更新]:
SELECT t2.studentID
from feesasigned t2
INNER JOIN (
SELECT t3.studentID, SUM(t3.remittedAmt) FeeSum
FROM remittedfees t3
GROUP BY t3.studentID) v ON t2.studentID = v.studentID and t2.fees = v.FeeSum
也许你正在寻找:
SELECT t2.studentID FROM feesasigned t2
INNER JOIN
(select sum(t3.remittedAmt) as remitted_total, t3.studentID from remittedfees t3
GROUP BY t3.studentID) t4
ON
t2.fees = t4.remitted_total AND t2.studentId = t4.studentID
它返回已支付所有费用的学生的ID您要检索的字段是什么?请使用数据添加有关表结构的更多信息。我认为b.费用应该是分组的一部分,否则可能会引发错误。您还可以删除汇款费用表的额外连接并使用总和(a.汇款金额)
:)当您执行求和(列),第2列
时,您需要使用按列分组2
,请更新您的查询:)