【问题标题】:How to remove/filter out records from select * from tableName query?如何从 select * from tableName 查询中删除/过滤记录?
【发布时间】:2021-07-05 10:43:36
【问题描述】:

如果我运行 select * from tableName 查询,我会得到以下记录:

id   Name

1    Lorum
2    Ipsum
3    Dolor
4    Sit
5    Amet

从这些记录中,如果我使用 say: 'Dolor' 那么查询应该只返回 id3 的其余记录,如下所示:

id   Name

1    Lorum
2    Ipsum
4    Sit
5    Amet

我无权访问该表,也无法删除或更新。有没有办法从查询中删除我们正在使用的记录,只返回未使用的记录?

现在我如何删除一条记录。喜欢:select * from tableName where Name != 'Dolor'

问题是我将要获取数百条记录,但我不确定如何动态执行上述操作。我已经创建了一个表来保存我将使用的记录,所以我在这里寻求帮助,以了解如何通过将它们与该表中的记录匹配来从查询中过滤/删除 select * 中的记录我已经已创建。

感谢您的帮助。

【问题讨论】:

  • select * from tableName where Name not in (select name from tempTableName)
  • 或者可能(更好地)使用EXISTS 以便以更“预期”的方式处理NULL 值?

标签: sql sql-server


【解决方案1】:

如果有你不想要的名字列表,你可以使用not in

select *
from tableName
where Name not in ('Dolor', 'Lorem');

【讨论】:

    【解决方案2】:
    select * 
    from tableName 
    where Name <> 'Dolor'
    -------------------- or
    select * 
    from tableName 
    where Name not in ('Dolor','Amet')
    

    【讨论】:

    • 感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将有助于未来的读者更好地了解正在发生的事情,尤其是那些刚接触该语言并难以理解概念的社区成员。
    【解决方案3】:

    使用 LINQ,您可以执行以下操作:

    from t in tableName
    where Name != filteredName
    

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 2019-05-14
      • 2023-01-11
      • 2016-11-30
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多