SQL 删除与另一个 table

SQL Delete with relation to another table

我有两个表,例如:

包含列 GUID、ProgKey、UserKey、ProgName 的表 A。

包含 GUID、AppKey、ModeName 列的表 B。外键:GUID->TableA.GUID

我需要删除具有特定值的 TableB.AppKey,但前提是 TableA.UserKey= 特定值。

我试过像这样简单的方法,但没有用:

Delete from TableB b, TableA a where b.AppKey=? and a.UserKey=?

并尝试了这个,也没有用:

Delete from TableB (AppKey) Select ? From TableB b, TableA a where a.UserKey=?

?=appkeyValue000

?=userkeyValue000

GUID 是主键。

delete from table where id in (select id from table2 where y=234)?

您可以使用 JOIN 删除,这在我看来是最自然的方式。

DELETE TableB
FROM TableB JOIN TableA ON TableA.GUID = TableB.GUID
WHERE TableB.AppKey = <Value> AND TableA.UserKey = <OtherVal>;

http://dev.mysql.com/doc/refman/5.0/en/delete.html

DELETE tableB FROM tableB JOIN tableA ON (tableB.GUID = tableA.GUID)
WHERE tableA.UserKey = 'userkeyValue000'
AND tableB.AppKey = 'appkeyValue000'

删除发生在 FROM 关键字之前的 table 中。