Jos*_*ore 5 sql search many-to-many relational-division
我有两个主要的表的数据库notes和labels.它们具有多对多关系(类似于stackoverflow.com对标签的问题).我想知道的是如何使用SQL搜索使用多个标签的笔记?
例如,如果我有一个带有三个标签"one","two"和"three"的注释"test",我有第二个注释"test2",标签为"one"和"two",那么SQL查询将是什么找到与标签"一"和"两"相关的所有音符?
为了获得具有笔记细节都标记"一"和"二":
select * from notes
where note_id in
( select note_id from labels where label = 'One'
intersect
select note_id from labels where label = 'Two'
)
Run Code Online (Sandbox Code Playgroud)