【问题标题】:Issue of Select in MySql using NOT IN operator?使用 NOT IN 运算符在 MySql 中选择问题?
【发布时间】:2016-05-04 06:41:54
【问题描述】:

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

有什么帮助吗?提前致谢!

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 列

【问题讨论】:

  • 在 WHERE NOT IN clouse 中将 SELECT * 更改为 id...
  • 它对你有用吗?
  • 如果它工作,那么请接受我的回答...

标签: mysql


【解决方案1】:

您需要在IN 子句中指定列,而不是使用*

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

【讨论】:

    【解决方案2】:

    试试这些,我已经更改了 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
    

    【讨论】:

    • 我已接受,然后投票给我。
    【解决方案3】:

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

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

    【讨论】:

    • 非常有帮助的想法。谢谢。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2021-05-18
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多