You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL报错:未用EXISTS引入子查询时SELECT列表仅能指定一个表达式

SQL错误排查与修正

错误原因

报错的核心问题出在第三个CASE分支的IN子查询里:

当子查询未用EXISTS引入时,SELECT列表中只能指定一个表达式。

你写的第三个子查询同时返回了title和sum(qty*price)两个字段,但IN子查询要求必须仅返回单一列;而且你是用title_id in (...)做匹配,子查询应该返回title_id而非其他字段,逻辑上也不匹配。

修正后的SQL

select title, price, case 
    when pub_id in (
        select pub_id
        from publishers
        where state = 'ca') then price * 1.1
    when title_id in (
        select title_id
        from titleauthor
        group by title_id
        having count(au_id) > 1) then price * 1.05
    when title_id in (
        select t.title_id  -- 仅返回title_id,匹配外层的title_id
        from titles t
        inner join sales s on t.title_id = s.title_id
        group by t.title_id
        having sum(qty*price) > 500) then Price * 1.02                  
    else price * 1.01
    end NewPrice
from titles

关键调整点

  • 第三个子查询的SELECT子句改为仅返回t.title_id,确保IN子查询的返回列单一且匹配外层的title_id
  • 分组条件简化为t.title_id,完全满足统计需求,避免不必要的列干扰

内容的提问来源于stack exchange,提问作者amin beihaghi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 12:55:09