1

基本上,我有三张桌子

具有字段 id 的工作场所(我想删除其中一个关联数据)和一个用于标识参与者列表的参与者列表 ID。

参与者列表 - 只是 list_id 和名称

参与者列表链接 - 带有 list_id 和参与者 ID

我试图使用的查询是这样的:

DELETE FROM workplaces,
participantlist,
participantlist_links 
WHERE workplaces.id =  '8' AND 
      workplaces.participantlist_id = participantlist_links.list_id AND  
      workplaces.participantlist_id = participantlist.list_id

但我得到:

MySQL 说:

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 3 行的 'WHERE workspaces.id='8' AND workspaces.participantlist_id = 参与者' 附近使用正确的语法

有任何想法吗?

解决方案:

    DELETE t1, t2, t3
    FROM workplaces AS t1
    LEFT JOIN participantlist AS t2 ON t1.participantlist_id = t2.list_id
    LEFT JOIN participantlist_links AS t3 ON t2.list_id = t3.list_id
    WHERE t1.id = '8'
4

4 回答 4

2

您是否考虑过放置ON DELETE CASCADE外键?这样,当您从工作区中删除该行时,其他行也将被自动删除。

有关更多信息,请参阅此链接:http: //dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

否则,您必须使用单独的 DELETE FROM 查询将它们全部删除。

在执行这些删除时使用事务是明智的。因此,如果出现任何问题,您可以回滚您的更改:

BEGIN TRANSACTION;
DELETE FROM ......;
DELETE FROM ......;
COMMIT;
于 2012-05-19T08:07:59.893 回答
1

尝试

DELETE workplaces, participantlist, participantlist_links FROM workplaces,
participantlist,
participantlist_links 
WHERE workplaces.id =  '8' AND 
      workplaces.participantlist_id = participantlist_links.list_id AND  
      workplaces.participantlist_id = participantlist.list_id
于 2012-05-19T08:09:48.280 回答
1

手册中所述,多表形式的DELETE语法与您使用的语法不同。你要:

DELETE workplaces, participantlist, participant_links
FROM   workplaces
  JOIN participantlist
    ON workplaces.participantlist_id = participantlist.list_id
  JOIN participantlist_links
    ON workplaces.participantlist_id = participantlist_links.list_id
WHERE  workplaces.id = '8'
于 2012-05-19T08:11:28.157 回答
1

您可以使用 JOIN 从多个表中删除,如下所示:

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;

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

于 2012-05-19T08:15:43.630 回答