【问题标题】:Selecting identical items based on shared foreign IDs they have根据共享的外国 ID 选择相同的项目
【发布时间】:2019-09-03 09:08:47
【问题描述】:

数据库包含产品集合;每个收集到的产品都有一个在添加到集合时记录的价格,以及一些其他值。

// `collections_products`

id collection_id group product_id option_id price
1  1             0     56         0         3.1920
2  1             0     56         54        1.2000
3  1             0     56         55        2.4000
4  1             0     56         56        3.6000
5  1             0     56         57        4.8000
6  1             0     56         58        6.0000
7  1             0     57         0         3.1920
8  1             0     57         54        1.2000

11  10           0     56         0         3.1920
12  10           0     56         54        1.2000
13  10           0     56         55        2.4000
14  10           0     56         56        3.6000
15  10           0     56         57        4.8000
16  10           0     56         58        6.0000
17  10           0     57         0         3.1920
18  10           0     57         54        1.2000

21  100          0     56         0         9.9999
22  100          0     56         54        9.9999
23  100          0     56         55        9.9999
24  100          0     56         56        9.9999
25  100          0     56         57        9.9999
26  100          0     56         58        9.9999
27  100          0     57         0         9.9999
28  100          0     57         54        9.9999


31  1000         0     56         0         3.1920
32  1000         0     56         54        1.2000
33  1000         0     56         55        2.4000
34  1000         0     56         56        3.6000

36  1000         0     56         58        6.0000
37  1000         0     57         0         3.1920
38  1000         0     57         54        1.2000

有一些collection_id,我需要找到其他相同的、重复的集合(具有相同的内容,即相同价格的相同产品、组和选项;顺序不重要)。

在上面的例子中:

  • collection_id10 的行集(B 集)是collection_id1 的行集(A 集)的副本;对于 A 中的每一行,B 中的另一行具有相同的group product_id option_id price,并且 A 和 B 具有相同的行数
  • collection_id 100 的行集与其他行不重复,因为所有价格都不同
  • collection_id 1000 的行集不与任何其他行重复,因为行数不同(与collection_id 1 相比,缺少行 ID 35)

想出了:

  • 在一条 SQL 语句中执行一个选择查询,根据它们具有的常见 ID 和值来查找其他集合,但不确定这是否完全适用于 MySQL
  • 计算每个集合结果集的校验和(组、product_id、option_id、每行的价格、一起),将其存储为collections.checksum,每次集合内有移动时重新计算。搜索时,获取我拥有的集合的校验和并通过该校验和进行选择。

研究了校验和的想法。找到:

不想重新发明轮子。很惊讶我找不到任何可重复使用的东西,除非我看错了方向。

解决这个问题的正确方法是什么?请指教

更新我不想删除任何集合,即使它们是重复的。我需要将它们组合起来。这是一个半虚构的例子,如果它没有 100% 的意义,请见谅

【问题讨论】:

  • 您的意思是将整个集合与另一个集合匹配并看到它的机器人具有完全相同的行?
  • @aexl 检查这个答案:stackoverflow.com/a/57712189/2469308
  • 您肯定正在寻找具有相同 (group, product_id, option_id, price) 和不同 ID 的行,并且您只想保留最高 ID(最近的)?
  • @CaiusJard 不,我不是要删除重复项,而是实际合并它们。编辑问题以(希望)使其更加明显。很抱歉造成混乱
  • 组合是什么意思?把它们的价格加在一起?平均价格?

标签: mysql


【解决方案1】:

这样的事情应该可以工作:

SELECT `product_id`, `option_id`, `group`, `price`, COUNT(*) as count_occurrences 
FROM `collections_products`
GROUP BY `product_id`, `option_id`, `group`, `price`
HAVING count_occurrences > 1;

这将为您提供在数据集中多次出现的所有(product_id、option_id、price)组合。如果您还想要相关行的 ID,可以使用 JOIN 进行子查询,如下所示:

SELECT cp.`id` FROM
(SELECT `product_id`, `option_id`, `group`, `price`, COUNT(*) as count_occurrences 
FROM `collections_products`
GROUP BY `product_id`, `option_id`, `group`, `price`
HAVING count_occurrences > 1) t1
LEFT JOIN `collections_products` cp
ON t1.`product_id` = cp.`product_id` 
AND t1.`option_id` = cp.`option_id` 
AND t1.`group` = cp.`group`
AND t1.`price` = cp.`price`;

UPD:

要获取与给定集合包含相同产品的集合 ID,您需要以下内容:

SELECT DISTINCT t2.`collection_id` FROM
(SELECT `collection_id`,`product_id`, `option_id`, `group`, `price`
FROM `collections_products`
WHERE `collection_id`=?) t1
LEFT JOIN `collections_products` t2
ON t1.`product_id`=t2.`product_id`
AND t1.`option_id`=t2.`option_id`
AND t1.`group`=t2.`group`
AND t1.`price`=t2.`price`
AND t1.`collection_id`<>t2.`collection_id`;

【讨论】:

  • 感谢您的意见。抱歉,我在问题中遗漏了一些内容:我需要根据给定的 collection_id 搜索重复的集合。刚刚编辑过。
  • @aexl 我不确定我是否完全理解你的问题。我首先想到的是在这两种情况下都在FROM collections_products 行之后将WHERE colledtion_id=? 子句添加到查询中。这将为您提供特定集合中具有相同产品、选项、组和价格的所有行。这是你需要的吗?
  • 我需要找到与给定集合具有相同内容的其他集合。内容 = 相同的产品、选项、组和价格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多