所以我有3张桌子:
collections
(id
,name
)items
(id
,name
,description
,price
)collection_items
(id
,collection_id
,item_id
,order
)用户可以从collections
列表中创建自己的items
,但我需要一种方法来检查collection
是否包含所有items
(同一order
SELECT
*
FROM
collections
WHERE
collections.id IN(
SELECT
c.id
FROM
collections c
JOIN
collection_items ci ON c.id = ci.collection_id
WHERE
(ci.order = 1 AND ci.item_id = 75) OR (ci.order = 2 AND ci.item_id = 58)
)
1}})已存在 - 以防止重复。
在我意识到它不起作用之前,这就是我的尝试。
$(".next").click(function() {
如何以特定顺序返回任何具有特定ID的集合?
答案 0 :(得分:0)
这类似于How to return rows that have the same column values in MySql
中的解决方案SELECT ci.collection_id
FROM collection_items ci
WHERE
(ci.order = 1 AND ci.item_id = 75) OR (ci.order = 2 AND ci.item_id = 58)
GROUP BY ci.collection_id
HAVING COUNT(*) = 2
如果这返回一行,则表示存在重复。
HAVING
子句中的数字应该是您在WHERE
子句中检查的项目数。
答案 1 :(得分:0)
我认为您不需要查看集合 - 只需查看以下集合项:
select collection_id from collection_items ci
WHERE (ci.order = 1 AND ci.item_id = 75)
AND (ci.order = 2 AND ci.item_id = 58)
AND 2 = (select count(*) from collection_items innter_ci
where innter_ci.collection_id = ci.collection_id)
如果您不关心collection_item是否具有相同数量的项目,那么您不需要最后一个“AND”