Reputation: 1530
I can't figure out why this query won't work. I did a SELECT to verify it's pulling the rows that I want. I put in DELETE and now it won't work.
Here is my SELECT query:
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
I'm sure it's something stupid I'm overlooking. Little help is appreciated.
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
I also tried this query with a JOIN that did not work:
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
Error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as dh JOIN dragon_list as dl ON dh.dragon_list_id = dl.dragon_list_id WH' at line 2
I only want to clear the values in the dragon_to_habitat table. The dragon_list table should be untouched.
For good measure, here's my schema: http://sqlfiddle.com/#!2/7ac6a1
Upvotes: 0
Views: 111
Reputation: 637
You need to JOIN
the dragon_to_habitat
and dragon_list
table -
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
Upvotes: 2
Reputation: 61
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
Upvotes: 0
Reputation: 18767
Try using 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
Upvotes: 0