为什么这个DELETE查询不起作用?

时间:2014-06-09 06:02:52

标签: mysql

我无法弄清楚为什么这个查询不起作用。我做了一个SELECT来验证它是否正在拉动我想要的行。我输入DELETE,现在它不起作用。

这是我的SELECT查询:

SELECT dh.dragon_list_id, dh.habitat_list_id 
FROM dragon_to_habitat dh, dragon_list dl
  WHERE dh.dragon_list_id = dl.dragon_list_id
    AND dl.user_id = 1
    AND dl.is_deleted = false

我确定这是一个我忽视的愚蠢行为。很少得到帮助。

DELETE FROM dragon_to_habitat dh
  WHERE dh.dragon_list_id = dragon_list.dragon_list_id
    AND dragon_list.user_id = 1
    AND dragon_list.is_deleted = false

我还尝试使用无效的JOIN进行此查询:

DELETE 
FROM dragon_to_habitat as dh JOIN 
     dragon_list as dl ON dh.dragon_list_id = dl.dragon_list_id
WHERE dl.user_id = 1 AND dl.is_deleted = false

错误:#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,使用'dh JOIN dragon_list as dl ON dh.dragon_list_id = dl.dragon_list_id WH'在第2行

我只想清除dragon_to_habitat表中的值。 dragon_list表应该不受影响。

为了更好的衡量,这是我的架构:http://sqlfiddle.com/#!2/7ac6a1

3 个答案:

答案 0 :(得分:2)

您需要JOIN dragon_to_habitatdragon_list表格 -

DELETE FROM dragon_to_habitat AS dh
JOIN dragon_list AS dragon_list ON dh.dragon_list_id = dragon_list.dragon_list_id
WHERE dragon_list.user_id = 1 AND dragon_list.is_deleted = false

答案 1 :(得分:0)

尝试使用JOIN

DELETE 
FROM dragon_to_habitat as dh JOIN 
     dragon_list as dl ON dh.dragon_list_id = dl.id
WHERE dl.user_id = 1 AND dl.is_deleted = false

答案 2 :(得分:0)

DELETE FROM 
 dragon_to_habitat 
USING 
  dragon_to_habitat dh 
INNER JOIN 
  dragon_list dl 
ON 
  dh.dragon_list_id = dl.dragon_list_id 
AND 
  dl.user_id = 1 
AND 
  dl.is_deleted = false