【问题标题】:I am trying to run the following SQL code with EXISTS keyword, could you help me with where I might be going wrong我正在尝试使用 EXISTS 关键字运行以下 SQL 代码,你能帮我解决我可能出错的地方吗
【发布时间】:2020-03-16 23:50:42
【问题描述】:
select p.ProductID,
       p.ProductName,
       p.CategoryID
from   Products as p
where  p.CategoryID EXISTS (
         select *
         from   Categories as c
         where  c.CategoryID = p.CategoryID
       ) ;

错误:消息 156,级别 15,状态 1,行 2 关键字“EXISTS”附近的语法不正确。

【问题讨论】:

  • where p.CategoryID EXISTS (SELECT ...) 应该只是 where EXISTS (SELECT ...)。除此之外,查询应该可以工作。投票关闭拼写错误。

标签: mysql sql exists


【解决方案1】:

exists之前没有列名:

select p.ProductID, p.ProductName, p.CategoryID
from Products p
where exist (select 1
              from Categories c
              where c.CategoryID = p.CategoryID
             ) ;

我更喜欢select 1 作为子查询,因为它很容易输入。不过,select * 并没有错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多