简体   繁体   English

如何修复错误代码 1241:操作数应在 mysql 查询中包含 1 列?

[英]How do I fix Error Code 1241: Operand should contain 1 column(s) in mysql query?

select firstName, lastName from students, courses, registration
where students.studentID = registration.studentID 
and courses.courseCode = registration.courseCode 
and gender = 'M' and courseName = 'Database Systems' 
in(select firstName, lastName 
from students, courses, registration
where students.studentID = registration.studentID 
and courses.courseCode = registration.courseCode 
and gender = 'M' and courseName = 'C++');``

I need to find the male students who have taken both Database Systems and C++, to do this I need to left join the tables students, registration, and courses,我需要找到同时参加过数据库系统和 C++ 的男学生,为此我需要离开表格学生、注册和课程,

Your query fails because of the way you use the in operator, which requires a column name or an expression in the left side.由于您使用in运算符的方式,您的查询失败,这需要左侧的列名或表达式。

Based on your description of your goal, I suspect that your query could be rewritten to use exists condition with correlated subqueries for filtering, like so:根据您对目标的描述,我怀疑您的查询可以重写为使用exists条件和相关子查询进行过滤,如下所示:

select 
    firstName, 
    lastName 
from students s
where 
    gender = 'M'
    and exists(
        select 1
        from courses c
        inner join registration r on c.courseCode = r.courseCode 
        where 
            c.courseName = 'Database Systems' 
            and s.studentID = r.studentID 
    )
    and exists(
        select 1
        from courses c
        inner join registration r on c.courseCode = r.courseCode 
        where 
            c.courseName = 'C++' 
            and s.studentID = r.studentID 
    )   

Another possible solution would be to use aggregation, with a having clause for filtering:另一种可能的解决方案是使用聚合,并带有一个用于过滤having子句:

select s.firstName, s.lastName 
from students s
inner join registration r 
    on s.studentID = r.studentID 
inner join courses c 
    on  c.courseCode = r.courseCode 
    and c.courseName in ('Database Systems',  'C++' )
where s.gender = 'M'
group by s.studentID, s.firstName, s.lastName 
having count(distinct c.courseName) = 2

Your in clause is missing some column try您的 in 子句缺少一些列尝试

select 
   firstName, lastName 
from students, courses, registration
where students.studentID = registration.studentID 
and courses.courseCode = registration.courseCode 
and gender = 'M' and courseName = 'Database Systems' 
and students.studentID
in (select studentID 
from students, courses, registration
where students.studentID = registration.studentID 
and courses.courseCode = registration.courseCode 
and gender = 'M' and courseName = 'C++');

This query can be more easily written using a HAVING clause to check that the count of courses that the student has taken out of the set ('Database Systems', 'C++') is 2:使用HAVING子句可以更轻松地编写此查询,以检查学生从集合('Database Systems'、'C++')中选择的课程数量是否为 2:

SELECT s.studentID, s.firstName, s.lastName 
FROM students s
JOIN registration r ON s.studentID = r.studentID 
JOIN courses c ON c.courseCode = r.courseCode 
WHERE s.gender = 'M' AND c.courseName IN ('Database Systems', 'C++')
GROUP BY s.studentID, s.firstName, s.lastName
HAVING COUNT(DISTINCT c.courseName) = 2

Note I have rewritten your JOIN s in the preferred style with ON conditions.请注意,我已经用ON条件以首选样式重写了您的JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM