【发布时间】:2010-09-23 19:14:46
【问题描述】:
给定一组用户指定的标签,我如何使用 1 个 SQL 语句确定标签表中不是的标签?
假设一个表架构 tags (id, tag) 并且我正在使用 mysql,如果有我不知道的优化。
谢谢
【问题讨论】:
-
原来你不能用 1 个查询来做到这一点:(
给定一组用户指定的标签,我如何使用 1 个 SQL 语句确定标签表中不是的标签?
假设一个表架构 tags (id, tag) 并且我正在使用 mysql,如果有我不知道的优化。
谢谢
【问题讨论】:
select * from canonical_list_of_tags where tag not in (select tag from used_tags)
至少在用于 SQL Server 的 T-SQL 中有效......
编辑:假设表canonical_list_of_tags 填充了“给定用户指定标签的集合”的结果
【讨论】:
select
utt.tagName
from
userTypedTags utt
left join tag t on utt.tagName = t.tag
where
t.ID is null
and utt.userID = <ID of the User in question>
假设你有桌子
userTypedTags(userID, tagName)
我添加了related question
【讨论】:
SELECT Tag
FROM UserSpecifiedTags
LEFT OUTER JOIN AllTags ON UserSpecifiedTags.Tag = AllTags.Tag
WHERE AllTags.Tag IS NULL
这应该返回你想要的。以我的经验,执行联接并查找不匹配的行比使用 IN 运算符要快得多。
【讨论】: