删除查询,其中表中的数据不存在于其他表上

时间:2013-06-27 13:46:35

标签: php sql

删除表行的典型查询通常是这样的,对吗?

$query1 = odbc_exec($conn, "DELETE FROM table1 WHERE uname1 <> uname2");

但是,如果我想以uname2位于不同的表中的方式,那该怎么办?

2 个答案:

答案 0 :(得分:3)

DELETE FROM table1
WHERE uname1 NOT IN (
    SELECT uname2
    FROM table2
)

答案 1 :(得分:0)

如果uname1是唯一的,您可以执行以下操作:

DELETE FROM table1 AS t1
WHERE uname1 NOT IN (
    SELECT uname2
    FROM table2 AS t2
    WHERE t2.uname1 = t1.uname1
);