好的,首先让我介绍一下我的模式。
表:用户
Field | Type | Key | NN | AI
---------------------------------------------
id | int(11) | PRIM | YES | YES
username | varchar(50) | | YES | NO
banned | int(1) | | YES | NO
Foreign keys: none表: macs
Field | Type | Key | NN | AI
---------------------------------------------
id | int(11) | PRIM | YES | YES
address | varchar(50) | | YES | NO
lastacc | timestamp | | YES | NO
Foreign keys: none表: usermac
Field | Type | Key | NN | AI
---------------------------------------------
userId | int(11) | PRIM | YES | NO
macId | int(11) | | YES | NO
Foreign keys:
[FK: macId] references macId to id in table macs
[FK: userId] references userId to id in table users方案是,我有一个userId,希望删除与该userId链接的macs中的所有记录(如usermac中的记录所述)。
用户和mac之间的关系:用户可以拥有多个macs,而mac可以属于多个用户。
对此我应该执行什么SQL查询?我尝试过级联删除,但级联不起作用。
提前谢谢你的回答。
发布于 2014-08-02 20:03:27
DELETE FROM mac WHERE id in (SELECT macId from usermac WHERE userId = <userId of deleted User>); 希望这能有所帮助。
发布于 2014-08-02 19:58:06
越简单越好:
DELETE FROM usermac WHERE userId=1;将删除用户1的所有macs。
https://stackoverflow.com/questions/25098816
复制相似问题