2

In my database I have three tables: one contains words (word_id, word), the other lists categories (cat_id, cat_name) and third one assigns categories to all words (word_id, cat_id) (one or more categories per word).

Is it possible to delete all rows in the categories-to-words-table with a word_id that does not currently exist in the words table?

4

3 回答 3

4

是的,通过使用这个:

DELETE FROM categories_to_words
WHERE word_id NOT IN (SELECT word_id FROM words);

有更有效的方法可以做到这一点,但这会奏效。

但是:您应该真正使用外键进行调查,因为如果您使用它们,您将不会遇到您现在正在解决的问题。

于 2013-09-09T18:22:06.283 回答
1

最直接的方法是翻译你的句子:

delete from categories2words
    where word_id not in (select word_id from words);
于 2013-09-09T18:22:31.660 回答
1

尝试这个:-

Delete from categories2words
where word_id not in (select word_id from words);
于 2013-09-09T18:23:03.647 回答