为什么使用带有WHERE子句的删除操作会删除所有行?

huangapple go评论60阅读模式
英文:

Why id delete with where clause deleting all the rows?

问题

我正在尝试使用一个与另一个表相关的条件,从表A中删除一些行。
基本上是从表A中删除与表B中存在的行。

以下是我正在使用的查询:

delete Table_A from Table_A t1
join table_B t2
on t1.id = t2.id
where cndt_1 and cndt_2 and .... and cndt_7
-- where条件仅用于检查两个表的多个列的字段值是否相同

这个查询删除了表中的每一行,我无法理解为什么。我尝试过使用左连接执行相同的查询,结果相同。我还尝试过将where条件添加到on下面以及与where一起使用。在每种情况下,都会删除所有行。

英文:

I am trying to delete some rows from table A with the where condition referring to another table.
So basically deleting rows from table A also present in table B.

Following is the query I'm using:

delete Table_A from Table_A t1
join table_B t2
on t1.id = t2.id
where cndt_1 and cndt_2 and .... and cndt 7
-- The where conditions are just to check field values are same for multiple -- cols of the 2 tables

This query is deleting every single row present in table and I can't seem to understand why. I have tried the same query with left join too. Same result. I tried adding the where conditions just under on and also paired with where. In every case, it is deleting all the rows

答案1

得分: 1

以下是翻译好的部分:

"这很简单进行检查。只需运行以下查询:

SELECT *
from Table_A t1
join table_B t2
     on t1.id = t2.id
where cndt_1 and cndt_2 and .... and cndt 7

然后,您将获取要删除的行。然后,您可以删除一些条件或更改它们,以进一步筛选行。"

英文:

Well, this is very simple to check. Just run the following query:

SELECT *
from Table_A t1
join table_B t2
     on t1.id = t2.id
where cndt_1 and cndt_2 and .... and cndt 7

and you will get the rows you are going to delete. Then, you can remove some of your conditions or change them in order to filter the rows further.

答案2

得分: 0

我建议使用以下代码:

删除:

DELETE
FROM Table T
JOIN 
OtherTable Ot
ON
T.id=Ot.id
WHERE 
"这里是从两个表中定义要删除记录的具体条件。例如,T.name='Titanic'"

更新:

UPDATE T
SET T.column = "要更改的值"
FROM Table T
JOIN 
OtherTable Ot
ON
T.id=Ot.id
WHERE 
"这里是从两个表中定义的具体条件。例如,T.name='Titanic'"
英文:

I recommend this one:

This deletes

DELETE
From Table T
Join 
OtherTable Ot
On
T.id=Ot.id
Where 
"here are your specific conditions from both tables which defines records to delete . Eg. T.name='Titanic'"

This updates:

Update T
Set T.column = "your value you want to change"
From Table T
Join 
OtherTable Ot
On
T.id=Ot.id
Where 
"here are your specific conditions from both tables. Eg. T.name='Titanic'"

huangapple
  • 本文由 发表于 2023年2月24日 12:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定