简体   繁体   English

使用多个条件删除SQL

[英]Deleting in SQL using multiple conditions

Some background, I have a code column that is char(6). 在某些背景下,我有一个属于char(6)的代码列。 In this field, I have the values of 0,00,000,0000,000000,000000. 在此字段中,我的值为0,00,000,0000,000000,000000。 It seems illogical but that's how it is. 似乎不合逻辑,但事实就是如此。 What i need to do is delete all rows that possess these code values. 我需要做的是删除所有拥有这些代码值的行。 I know how to do it individually as such 我知道该怎么做

delete from [dbo.table] where code='0'
delete from [dbo.table] where code='00'

and so on. 等等。

How does one do this one section of code instead of 6 如何做到这一段代码而不是6

Try this: 尝试这个:

delete from [dbo.table] where code='0'
                           or code='00'
                           or code='000'

etc. You get the idea. 等。你明白了。

There can be more efficient ways when the set of vales gets larger, but your 5 or 6 values is still quite a ways from that. 当一组值变大时,可能会有更有效的方法,但从中获得5或6值仍然是相当有效的方法。

Update : 更新

If your list grows long, or if your table is significantly larger than can reside in cache, you will likely see a significant performance gain by storing your selection values into an indexed temporary table and joining to it. 如果列表变长,或者表的大小远远大于缓存中的表,则可以通过将选择值存储到索引的临时表并将其连接到表中来获得显着的性能提升。

It strongly depends on your DBMS, but I suggest to use regular expressions. 它在很大程度上取决于您的DBMS,但我建议使用正则表达式。 For example, with MySQL you just need simple query like this: 例如,对于MySQL,您只需要这样的简单查询:

delete from dbo.table where code regexp '(0+)'

For most of popular DBMS you can do the same, but syntax may be various 对于大多数流行的DBMS,您可以执行相同的操作,但是语法可能有所不同

I can't test it right now, but the following should work: 我目前无法对其进行测试,但是以下方法应该可以工作:

DELETE FROM dbo.table WHERE CONVERT(int, code) = 0

edit- Just thought of another way, that should be safer: 编辑-只是想了另一种方式,那应该更安全:

DELETE FROM dbo.table WHERE LEN(code) > 0 AND LEFT(code + '0000000000', 10) = '0000000000'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM