以下是两个表格:
accounts
+-----------+-----------+--------+
| accountid | account | userid |
+-----------+-----------+--------+
| 1 | blah1 | 1 |
| 2 | blah2 | 2 |
| 3 | blah3 | 3 |
| 4 | 2nd4user1 | 1 |
+-----------+-----------+--------+
links
+-------------+-----------+--------+
| usergroupid | accountid | userid |
+-------------+-----------+--------+
| 1 | 4 | 3 | //Links user 3 on user1's account 4.
| 2 | 2 | 3 | //Links user 3 on user 2's account 2.
| 3 | 3 | 1 | //Links user 1 on user 3's account 3
+-------------+-----------+--------+
是否可以根据已知的usergroupid和已知的userid编写可以删除的单个sql查询,其中userid可以位于两个表的 中。
我使用左连接列出了每个用户的所有帐户,然后有一个基于usergroupid的删除链接。对于其中的每一个,userid将是相同的,但usergroupid将不同。
例如,组合:
Delete from table2 where usergroupid = 3 and userid = 1.
(从用户3&#39的帐户中移除用户1)
AND
DELETE from table2 where usergroupid = 1 and accounts.userid = 1
(从用户1帐户4中删除用户3)
我已尝试内部加入左连接并选择不同,但无法使其工作!显然,我可以写两个不同的查询,但我真的想一气呵成 - 如果可以的话。我究竟做错了什么?
DELETE links FROM links INNER JOIN accounts ON accounts.accountid =
links.accountid WHERE links.usergroupid = 30 AND (accounts.userid = 1 OR
links.userid = 1) LIMIT 1
DELETE links FROM links ON 'accountid' IN (SELECT 'accountid' FROM 'accounts')
WHERE links.usergroupid = :usergroupid AND (accounts.userid = :userid OR
links.userid = :userid)
DELETE FROM links WHERE 'accountid' IN (SELECT 'accountid' FROM 'accounts'
INNER JOIN 'links' on 'accounts.accountid' = 'links.accountid') WHERE
'links.usergroupid' = :usergroupid AND ('accounts.userid' = :userid OR 'links.userid' = :userid)
LIMIT 1
DELETE FROM t1 USING links t1 INNER JOIN accounts t2 ON (t1.accountid =
t2.accountid) WHERE WHERE links.usergroupid = :usergroupid AND (accounts.userid = :userid OR
links.userid = :userid) LIMIT 1