使用NOT IN运算符在MySql中选择问题?

时间:2016-05-04 06:41:55

标签: mysql

我使用了以下mysql查询来选择所有选择的信息,其中学生表的学生ID不存在于费用共享表中但我遇到了错误。

有任何帮助吗?在此先感谢!

select st.id as 'Student ID', st.stud_fname as 'First Name', st.stud_lname as 'Last Name', st.stud_middle_name as 'Middle Name',
 dp.dep_name as 'Department',dp.max_dur_year as 'Max Duration', st.entry_year as 'Entry',MAX(sc.acc_year) as 'Current Academic Year',  
 sum(sc.Tuition_fee+sc.Accomod_fee+sc.Food_fee) as 'Total Cost Sharing'

from student st left JOIN student_costsharing sc on st.id = sc.stud_id
left join department dp on st.dep_id=dp.id 

 where st.id not in ( SELECT *              
        FROM student_costsharing 
        WHERE sc.stud_id=st.id
               )

GROUP BY st.stud_fname
order by st.stud_fname

错误消息:

  

1241 - 操作数应包含1列

3 个答案:

答案 0 :(得分:2)

您需要在*子句

中指定列,而不是使用IN
 where st.id not in ( SELECT stud_id              
        FROM student_costsharing 
        WHERE sc.stud_id=st.id
               )

答案 1 :(得分:1)

尝试这些,我更改了WHERE NOT IN语句: -

select st.id as 'Student ID', st.stud_fname as 'First Name', st.stud_lname as 'Last Name', st.stud_middle_name as 'Middle Name',
 dp.dep_name as 'Department',dp.max_dur_year as 'Max Duration', st.entry_year as 'Entry',MAX(sc.acc_year) as 'Current Academic Year',  
 sum(sc.Tuition_fee+sc.Accomod_fee+sc.Food_fee) as 'Total Cost Sharing'

from student st left JOIN student_costsharing sc on st.id = sc.stud_id
left join department dp on st.dep_id=dp.id 

 where st.id not in ( SELECT stud_id              
        FROM student_costsharing 
        WHERE sc.stud_id=st.id
               )

GROUP BY st.stud_fname
order by st.stud_fname

答案 2 :(得分:1)

问题是,子查询的结果集应该只包含一列,因为您无法将where子句的操作数与表的整个结果集进行比较。 哪个列应与id进行比较?

要解决此问题,您需要选择要与您的值进行比较的表格的一列。