mysql - 在此查询中出现错误 "Operand should contain 1 column(s)"

标签 mysql sql

所以我有下面的代码:

SELECT student_name FROM student S WHERE S.student_id IN
(SELECT t1.student_id, SUM(IF(t2.test_date IS NULL, 0, 1)) AS increase_score
FROM
test t1
LEFT JOIN test t2 
     ON t1.student_id = t2.student_id
     AND t1.test_date < t2.test_date
     AND t1.Score <= t2.Score
GROUP BY t1.student_id
HAVING 
     increase_score = 0
AND count(*) > 1)

我收到错误消息“操作数应包含 1 列”。这仅在添加外部 SELECT 语句后出现。我已经确认内部查询按预期工作。我看过其他一些涉及相同错误的示例,但我无法确定在这种情况下该怎么做才能修复它。

最佳答案

错误很明显:您的子查询中不能有多个字段,很明显,您是在针对单个列处理一个字段。 所以只需移动 HAVING 子句中的 SUM 即可:

SELECT student_name
FROM student S
WHERE S.student_id IN
(SELECT t1.student_id
FROM test t1
LEFT JOIN test t2 
     ON t1.Name = t2.Name
     AND t1.Date < t2.Date
     AND t1.Score <= t2.Score
GROUP BY t1.Name
HAVING 
     SUM(IF(t2.test_date IS NULL, 0, 1)) = 0
AND count(*) > 1)

http://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html

关于mysql - 在此查询中出现错误 "Operand should contain 1 column(s)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530047/

相关文章:

mysql - 正常条目的唯一条目?

MySQL - 将两列中具有相等值的多行视为一行

php - 更好的AWS实例

mysql - PHPStorm 无法连接到本地 MySQL

java - 加载数据查询不适用于 INSERT/REPLACE 选项

mysql - 如何在 MYSQL 中更新/使 View 可更新

PHP & MySQL : Insert records in two different tables

mysql - 如何在 MySQL 中将多查询解决方案编写为单个查询

sql - 我发现了一个异常(exception)!!怎么办?

mysql - 在 mySQL 中连接今天和昨天的列