Mysql 选择未标记的文档
假设我的数据库有3个表:Mysql 选择未标记的文档,mysql,join,Mysql,Join,假设我的数据库有3个表: Documents Tags Documents_Tags (the join table) 我知道如何使用左连接查找分配给文档的标记,但我很难找到未分配的标记 SELECT * FROM `documents_tags` LEFT JOIN `tags` ON `tags`.`id` = `documents_tags`.`tag_id` WHERE `document_id` = 111; 我尝试了不同的连接,但我一直只有一张记录。我认为有一种方
Documents
Tags
Documents_Tags (the join table)
我知道如何使用左连接查找分配给文档的标记,但我很难找到未分配的标记
SELECT * FROM `documents_tags`
LEFT JOIN `tags` ON `tags`.`id` = `documents_tags`.`tag_id`
WHERE `document_id` = 111;
我尝试了不同的连接,但我一直只有一张记录。我认为有一种方法可以连接所有标记,然后将结果限制在文档为空的位置
编辑:在上面的示例中,我需要查找未分配给文档
111的所有标记
,但在上面的查询中,您试图查找实际分配给文档111的所有标记
SELECT Tags.name -- or whatever it is
FROM
tags
LEFT JOIN documents_tags dt ON (tags.id = dt.tag_id AND dt.document_id = 111)
WHERE dt.id IS NULL