【问题标题】:"not" in query syntax [closed]查询语法中的“不”[关闭]
【发布时间】:2015-02-17 06:58:34
【问题描述】:

我有疑问

select *from dbo.seat
where noseat not in 
(select noseat from dbo.booking where statusbooked = 1)

我把它放在我的 .aspx.cs 文件中,但它显示错误。

conn.Open();
        string sql = "select *from dbo.seatwhere noseat not in ( select noseat from dbo.booking where statusbooked = 1)";
        cmd = new SqlCommand(sql, conn);
        dr = cmd.ExecuteReader();

关键字“not”附近的语法不正确。

这是我的桌子open this

有什么问题?

【问题讨论】:

  • 您代码中的查询与问题中的上述查询不同。状态预订字段。
  • 你忘了空格:dbo.seatwhere
  • 如果下面的答案解决了你的问题,你真的必须在发帖之前认真醒来:p
  • @YoupTube 恰好发生在我们当中 =)

标签: c# sql asp.net sql-server-2008


【解决方案1】:

为避免此类愚蠢错误(在第一个where之前遗漏空格)格式化您的代码:

using (SqlConection conn = new SqlConection(yourConnectionStringHere)) {
  conn.Open();

  // SQL should be readable...
  string sql = 
    @"select *
        from dbo.seat
       where noseat not in (
               select noseat 
                 from dbo.booking 
                where statusbooked = 1)";

  // All disposable instances should be properly disposed...
  using(var cmd = new SqlCommand(sql, conn)) {
    // All disposable instances should be properly disposed...
    using(var dr = cmd.ExecuteReader()) {
      while (dr.Read()) {
        ...
      } 
    }
  }
}

【讨论】:

    【解决方案2】:

    不要在不考虑子选择中的 NULL 值的情况下执行 NOT IN,因为如果其中有一个(或多个),则 NOT IN 为 NULL,并且查询根本不返回任何行 - 可能不是大多数人期待...

    在子选择中添加 IS NOT NULL:

    select * from dbo.seat
    where noseat not in 
    (select noseat from dbo.booking
     where statusbooked = 1
       and noseat IS NOT NULL)
    

    或者,使用 NOT EXISTS 代替(这是“空安全”):

    select * from dbo.seat s
    where NOT EXISTS
    (select 1 from dbo.booking b
     where statusbooked = 1
       and b.noseat = s.noseat)
    

    【讨论】:

    • 你为什么要select 1?我不明白。你介意解释一下吗? @jarlh
    • 在 EXISTS/NOT EXISTS 子查询中选择什么并不重要,重要的是是否找到了一行。
    【解决方案3】:

    您缺少空间wherenoseat

    string sql = "select *from dbo.seat where noseat not in ( select noseat from dbo.booking where statusbooked = 1)";
    

    【讨论】:

      【解决方案4】:

      在表名和where关键字之间加空格

          select *from dbo.seat where noseat not in 
              ( select noseat from dbo.booking where statusbooked = 1)
      

      【讨论】:

      • * & from 之间的空格怎么样?!
      • @YoupTube:不会产生错误。
      【解决方案5】:

      缺少空格: 1. 表名和鼻子列。 2. 在 * 和 from 之间

      string sql = "select * from dbo.seat where noseat not in (select noseat from dbo.booking where statusbooked = 1)";

      【讨论】:

        【解决方案6】:
        1. 您需要在 * 和 from 之间留出空格。 (可选但始终是很好的做法。)

        2. 表名和where关键字之间有空格

        3. statusbooked 到 statusbooking 或 statusbooking 到 statusbooked。

          string sql = "select * from dbo.seat where noseat not in 
          ( select noseat from dbo.booking where statusbooked = 1)";
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-04
          相关资源
          最近更新 更多