sql - 代码错误。未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式

标签 sql syntax-error

我正在尝试运行此查询,但是无论我做什么,我都会不断收到此错误,但无法解决。任何帮助,将不胜感激。

select
  CC.ContactID, 

from 
  Client C 
  join Contacts CC on CC.ContactID = C.ContactID 
 where 

        ClientID In (
    
                                  Select 
                                    ClientID,Sum(Total-allocated) as Bal 
                                  from 
                                    Main 
                                  Where 
                                    Total <> Allocated
                                    and NomTypeID < 10 
                                  Group By 
                                    ClientID
                                  HAVING Sum(Total-allocated) > 10
                             )

最佳答案

我不确定您使用的是哪种数据库(因此我也无法测试我的答案)。
但是通常,当您使用IN时,您需要子查询仅返回一列。
因此查询应为(删除了Sum(Total-allocated) as Bal):

select
  CC.ContactID, 

from 
  Client C 
  join Contacts CC on CC.ContactID = C.ContactID 
 where 

        ClientID In (
    
                                  Select 
                                    ClientID 
                                  from 
                                    Main 
                                  Where 
                                    Total <> Allocated
                                    and NomTypeID < 10 
                                  Group By 
                                    ClientID
                                  HAVING Sum(Total-allocated) > 10
                             )

关于sql - 代码错误。未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62909446/

相关文章:

php - PHP,注意: Undefined variable, how to fix?

EXCEL VBA : calling an event with onclick property of a button which is being created on the fly

SQL Server 将 Table1 中的所有行与 Table2 中的所有行进行匹配

mysql - 是否可以更改 mysql sgbd 上每个数据库的特定表?

mysql - 使用 phpMyAdmin 导出更改时间戳

mysql - 如果列值为空,则使用其他列

javascript - 未捕获的语法错误 : missing ) after argument list - JavaScript

java.sql.SQLException : Table/View 'LOCATION' already exists in Schema 'APP'

batch-file - 设置语法批处理脚本错误

sql - 为什么我会收到错误消息 "TOP or FETCH clause contains an invalid value"?