使用联接删除

时间:2013-10-23 05:00:04

标签: mysql sql

我无法弄清楚如何删除属于特定文件的单词。我正在尝试使用fileId 42删除属于该文件的单词。有人认为他们可以帮助我吗?

Table file
fileId - Primary Key
fileName - Normal column.
fileUrl - Normal column.

Table fileWord
fileId, wordId - Primary Key, Foreign Key
count - Normal column.

Table word
wordId - Primary Key
word - Normal Column

我的SQL:

DELETE words
FROM files f, fileword fw, words w
WHERE 42 = fw.fileId AND fw.wordId = w.wordId;

2 个答案:

答案 0 :(得分:2)

您是否尝试仅从“文件”表中删除该特定文件。如果是,请尝试以下代码,

DELETE
FROM files f, fileword fw, words w
WHERE f.fileId = 42
and f.fileId = fw.wordId 
and f.fileId = w.wordId;

答案 1 :(得分:2)

试试这个:

DELETE f FROM files as f
inner join fileword as fw on f.fileId = fw.wordId 
inner join words as w on f.fileId = w.wordId
WHERE f.fileId = 42

如果你想删除单词表中的单词,那么

DELETE w FROM files as f
inner join fileword as fw on f.fileId = fw.wordId 
inner join words as w on fw.wordId = w.wordId
WHERE f.fileId = 42