[英]Mysql Error: Subquery returns more than 1 row
Hi I have a mysql query to fetch a number of id's(studentID), given below 嗨,我有一个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 remittedfees t3
where t3.studentID = t1.studentID);
but the query returns the following error 但查询返回以下错误
ERROR 1242 (21000): Subquery returns more than 1 row
How can I rewrite the query to get the result ? 如何重写查询以获得结果?
Try this [updated]: 试试这个[更新]:
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
Most likely the select t2.fees
query is doing just as the error says - returning more than one row. select t2.fees
查询最有可能像错误所说的那样做-返回多个行。 When doing an equality comparison like that, both sides of the =
have to be single values. 当进行这样的相等比较时,
=
两端都必须是单个值。 If one side returns 2+ values, then you end up with 如果一侧返回2个以上的值,则结果为
1 = 1
2
3
ok... what's equal? 好...什么相等? 1=1?
1 = 1? 2=1?
2 = 1? 3=1?
3 = 1? which single 'truth' value should be used as the result of the comparison?
比较结果应使用哪个“真实”值?
I think you want to retrieve StudentID
whose fees
from feesasigned
table is queal to the total remittedAmt
from remittedfees
table. 我认为,要检索
StudentID
,其fees
由feesasigned
表queal总remittedAmt
从remittedfees
表。 Then try this: 然后试试这个:
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)
Maybe you are looking for: 也许您正在寻找:
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
Which returns the IDs of the student who has paid all their fees 这将返回已支付所有费用的学生的ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.