英文:
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'"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论