mySQL查询 - 我有一个表'数据库'。'table_product_options'包含大约1000行。此表应包含每个'product_id'的4x行 - 分配给每个'customer_group'的相同产品(如下所示)。这相当于每个customer_group约250行。
但是,我已经在每个'customer_group'上运行了一个COUNT,并且发现有一些 a + c c c em>(组 b + d 各为250行,但组 a + c 仅为245行)。
+----------------+----------------+
| product_id | customer_group |
+----------------+----------------+
| 1 | a | }
| 1 | b | }
| 1 | c | }-each 'product_id' has 4x 'customer_group' attached
| 1 | d | }
| 2 | a |
| 2 | b |
| 2 | c | << 1 missing entry (customer_group = d)
| 3 | a | << 3 missing entries (customer_group = b, c, d)
+----------------+----------------+
如何运行SELECT查询(或替代方案)以显示'product_id'分配的行数少于4行?或者,换句话说,我如何找到缺失的条目?
我尝试过很多不同的查询,包括SELECTs和WHERE NOT EXIST等等,但我的想法已经用完了。我修改了十几个stackoverflow答案,但没有一个是非常合适的,并且无法修改语法。
任何帮助都将受到高度赞赏(因为我预计将来会出现同样的问题......)。我最终可能会用眼睛擦桌子了!
答案 0 :(得分:0)
select product_id, count(customer_group)
from table_product_options
group by product_id
having count(customer_group) < 4
您可能需要逐行切换。我的订单很混乱。
答案 1 :(得分:0)
撰写寻找遗漏小组的可能组合。
select product_id, indicator, case when indicator = 1
case when availe = 'a' then 'b,c,d'
when availe = 'b' then 'a,c,d'
when availe = 'c' then 'a,b,d'
else 'a,b,c' end
case when indicator = 2
case when availe = 'a,b' then 'c,d'
//Like wise Give the possible combination for finding missing group
end as missing_group
from
(select product_id, count(customer_group) as indicator, group_concat(customer_group) as availe
from table_product_options
group by product_id) a where indicator < 4