SQL删除指定范围内的记录

发布于 2024-12-17 04:56:21 字数 173 浏览 7 评论 0原文

对于有经验的人来说,这可能是一个非常简单的问题,但我只是想知道删除 SQL 表中特定范围内的数百条记录的最安全方法。

例如,我需要删除 ID 介于 79 和 79 之间的行。 296:

我担心的是,如果我说删除具有 ID(> 79 且 < 296)的所有内容,那么它可能会真正擦除整个表。

This is probably a very simple question for somebody with experience, but I just wanted to know the safest way to delete a couple of hundred records in an SQL table that fall between a specific range.

For example I need to delete rows with an ID between 79 & 296:

My worry is if I say delete everything with an ID (>79 AND < 296) then it may literally wipe the whole table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

來不及說愛妳 2024-12-24 04:56:21

如果您使用 Sql Server

delete from Table where id between 79 and 296

注意:Between 语句包含在内,因此 79296 行也将被删除

编辑后您现在澄清了您想要:

ID (>79 AND <296)

所以使用这个:

删除来自表,其中 id > 79并且id< 296

If you use Sql Server

delete from Table where id between 79 and 296

Note : the between statement is inclusive, so rows 79 and 296 will also be deleted

After your edit : you now clarified that you want :

ID (>79 AND < 296)

So use this :

delete from Table where id > 79 and id < 296

一桥轻雨一伞开 2024-12-24 04:56:21

您给出了条件 ID(>79 且 <296),那么答案是:

delete from tab
where id > 79 and id < 296

这与:

delete from tab
where id between 80 and 295

如果 id 是整数相同。

所有人都回答:

delete from tab
where id between 79 and 296

这与:

delete from tab
where id => 79 and id <= 296

注意区别。

You gave a condition ID (>79 and < 296) then the answer is:

delete from tab
where id > 79 and id < 296

this is the same as:

delete from tab
where id between 80 and 295

if id is an integer.

All answered:

delete from tab
where id between 79 and 296

this is the same as:

delete from tab
where id => 79 and id <= 296

Mind the difference.

葬花如无物 2024-12-24 04:56:21
DELETE FROM table_name 
WHERE id BETWEEN 79 AND 296;
DELETE FROM table_name 
WHERE id BETWEEN 79 AND 296;
巷子口的你 2024-12-24 04:56:21

您也可以将删除更改为 select *

并测试您的选择,

所选记录将与删除的记录相同,

如果您不确定,您也可以将语句包装在开始/回滚中 - 测试语句,然后如果全部都是很好的删除回滚,

例如

SELECT * FROM table
WHERE id BETWEEN 79 AND 296

将显示与 where 匹配的所有记录
如果它们是您“真正”想要删除的内容,则使用

DELETE FROM table
WHERE id BETWEEN 79 AND 296

您还可以创建一个触发器 / 来捕获删除并将它们放入历史表中,

这样如果您错误地删除了某些内容,您总是可以将其恢复

(保持您的历史表记录不超过 6 个月或其他)业务规则说)

you can also just change your delete to a select *

and test your selection

the records selected will be the same as the ones deleted

you can also wrap your statement in a begin / rollback if you are not sure - test the statement then if all is good remove rollback

for example

SELECT * FROM table
WHERE id BETWEEN 79 AND 296

will show all the records matching the where
if they are the wants you 'really' want to delete then use

DELETE FROM table
WHERE id BETWEEN 79 AND 296

You can also create a trigger / which catches deletes and puts them into a history table

so if you delete something by mistake you can always get it back

(keep your history table records no older than say 6 months or whatever business rules say)

泼猴你往哪里跑 2024-12-24 04:56:21

如果您在 SQL Server 中按如下方式编写,则不会有擦除数据库表的危险,除非该表中的所有值实际上都在这些值之间:

DELETE FROM [dbo].[TableName] WHERE [TableName].[IdField] BETWEEN 79 AND 296 

If you write it as the following in SQL server then there would be no danger of wiping the database table unless all of the values in that table happen to actually be between those values:

DELETE FROM [dbo].[TableName] WHERE [TableName].[IdField] BETWEEN 79 AND 296 
焚却相思 2024-12-24 04:56:21

我担心的是,如果我说删除带有 ID 的所有内容(> 79 且 < 296)
那么它可能会擦掉整个桌子......

这种情况不会发生,因为您将有一个 where 子句。发生的情况是,如果您有类似 delete * from Table1 where id Between 70 and 1296 的语句,sql 查询处理器要做的第一件事就是扫描表并查找其中的记录范围,然后应用删除。

My worry is if I say delete evertything with an ID (>79 AND < 296)
then it may literally wipe the whole table...

That wont happen because you will have a where clause. What happens is that, if you have a statement like delete * from Table1 where id between 70 and 1296 , the first thing that sql query processor will do is to scan the table and look for those records in that range and then apply a delete.

白龙吟 2024-12-24 04:56:21

您可以使用这种方式,因为 id 不可能在所有情况下都是连续的。

SELECT * 
FROM  `ht_news` 
LIMIT 0 , 30

You can use this way because id can not be sequential in all cases.

SELECT * 
FROM  `ht_news` 
LIMIT 0 , 30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文