【问题标题】:could not prepare statement (1 misuse of aggregate function COUNT())无法准备语句(1 次滥用聚合函数 COUNT())
【发布时间】:2021-07-23 13:19:12
【问题描述】:

我正在尝试运行以下查询:

select U.userEmail, U.firstname + ' ' + U.surName as fullname 
from tblUser as U inner join tblHitchhhiker as H on U.userEmail= H.userEmail
left outer join tblGetAsk as G  on H.userEmail = G.userEmail
where COUNT(G.userEmail) = 0

但我收到以下错误:

错误 1:无法准备语句(1 滥用聚合函数 计数())

我真正想做的是:只选择也是搭便车者并且在 tblGetAsk 中有 0 条记录的用户。

【问题讨论】:

  • 这是哪个 DBMS?请适当地标记它。
  • Select only users that are also an hitchhiker and that have 0 records in tblGetAsk...尝试NOT IN查询
  • @ADyson W3Schools 在线 SQL Tryit 编辑器
  • @ADyson 会这样吗?:从 tblUser 中选择 U.userEmail,U.firstname + ' ' + U.surName 作为全名作为 U 内部连接 ​​tblHitchhhiker 作为 H on U.userEmail= H.userEmail where U.userEmail 不在(从 tblGetAsk 中选择 userEmail)
  • online SQL Tryit Editor of W3Schools...对于哪个数据库系统?它有针对各种不同 SQL 变体的课程。

标签: sql


【解决方案1】:

...在 tblGetAsk 中有 0 条记录

这意味着tblGetAsk 中没有与tblUseruserEmail 匹配的行,因此您的条件应该是where G.userEmail IS NULL

select U.userEmail, U.firstname + ' ' + U.surName as fullname 
from tblUser as U 
inner join tblHitchhhiker as H on U.userEmail= H.userEmail
left outer join tblGetAsk as G on U.userEmail = G.userEmail
where G.userEmail IS NULL

您也可以使用EXISTSNOT EXISTS 代替连接:

select U.userEmail, U.firstname + ' ' + U.surName as fullname 
from tblUser as U 
where exists (select 1 from tblHitchhhiker as H where H.userEmail= U.userEmail)
  and not exists (select 1 from tblGetAsk as G where G.userEmail = U.userEmail)

【讨论】:

猜你喜欢
  • 2021-09-30
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 2021-02-01
  • 2016-10-15
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多