gpt4 book ai didi

mysql - 如何通过另一个表检查 "duplicates"?

转载 作者:行者123 更新时间:2023-11-30 21:47:36 25 4
gpt4 key购买 nike

所以我有 3 个表:

  • 集合 (id, name)
  • 商品(id名称描述价格)<
  • collection_items (id, collection_id, item_id, order)<

用户可以从 items 列表中创建自己的 collection 但我需要一种方法来检查 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)
)

如何按特定顺序返回具有特定 ID 的任何集合?

最佳答案

这类似于 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 子句中检查的项目数。

关于mysql - 如何通过另一个表检查 "duplicates"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com