SQL Deleting while checking data from another table
I have 2 tables, words
and lists
.
words
belong to lists and are references by listId
field.
lists
contain a username
field.
I need to delete a word v开发者_开发百科ia it's id
but first need to check that the username is correct also, therefore I need to reference the lists table.
This is what I have so far but it does not work.
DELETE FROM w
USING lists AS l INNER JOIN words AS w
WHERE
l.username = 'admin'
AND w.listId = 31
AND l.id = w.listId
DELETE w FROM words w
INNER JOIN lists l on l.id = w.listid
WHERE
l.username = 'admin'
AND w.listId = 31
I'm not 100% sure about my-sql syntax (I use Oracle), but this should work:
DELETE FROM words
WHERE listid in (
SELECT 31 from lists where username = 'admin'
)
...if you really like, you could change the inner select to:
SELECT listId from lists where username = 'admin' and listId = 31
(but it's not necessary)
精彩评论