發表文章

目前顯示的是 3月 27, 2019的文章

Many-To-Many select only rows with exactly same tags

圖片
1 I have 3 tables: tags, products and relation table between them. Relation table looks for example like this: tagId | ProductId 1 | 1 2 | 1 2 | 9 The user can pick two options " All of these " or " One of these ". So if user picks All of these , it's means that the product must have exactly all of tags which the user chose. So if user pick tags with id 1 and 2 , it should select only product with id 1 , because this product has exactly the same tags the user chose. (Another way is if the user picks the tag with id 2 , it should select only product with id 9 .) So, the product has to have all tags which the user chose (no more, no less). SQL that I already have for Any/One of these: SELECT DISTINCT s.SKU FROM SKUToEAN as s LEFT JOIN ProductDetails as p ON s.ProductDetailID=p.id JOIN ProductTagRelation as ptr ON (ptr.productId=p.id and ptr.tagId IN(Ids of selected tags)) Example behavior: TagId = 1 it should select => None TagId = 2 it shoul