从table1删除其中column1大于table2.column1乘以2

时间:2015-08-01 07:13:08

标签: mysql mysqli

你可以看到很棒的标题。

尝试删除其中一列的值与另一个表的值相同的行。

select DB
delete 
from table1
where table1.id IN (select table2.id where table1.price > table2.price*2)

我希望该查询与id column匹配,并删除所有table1table1.column1,其行数是table2.column1中对等行的两倍。 我应该使用什么语法?

4 个答案:

答案 0 :(得分:0)

您需要加入子查询中的表。下面的示例假设两个表都有一个名为' id'

的唯一ID列
delete 
from table1
where table1.id IN (
select table2.id from table1, table2 where 
table1.id = table2.id
and
table1.price > (table2.price*2))

答案 1 :(得分:0)

table1中匹配id并且price中的条件table1正在删除大于table2的{​​{1}}

DELETE FROM table1
FROM   table1 INNER JOIN ON
table1 .id = table2.id
WHERE  (table1 .price  > table2.price* 2)

答案 2 :(得分:0)

如果这两个表可以通过他们的id加入,那就这样做:

select DB
delete 
from table1
where table1.id IN (
  select table2.id from table1 inner join table2 on table2.id=table1.id
  where table1.price > table2.price*2)

答案 3 :(得分:0)

delete from table1 where id in 
(select id from table1 join table2 on table1.id = table2.id
where table1.column1 > 2*table2.column1)