我有以下表格:
表:产品
--------------------------
id_product | name_product
--------------------------
1 | p1
2 | p2
3 | p3
表:数字
-----------------------
imei | id_product
-----------------------
56789 | 1
56799 | 2
56713 | 3
表:操作
----------------------
imei | date
------------------
56789 |31-07-2017
最后我想得到这个结果:
---------------------------------------
name_product | number_operation
---------------------------------------
p1 | 1
p2 | 0
p3 | 0
谢谢你
答案 0 :(得分:0)
这将按产品计算操作次数:
select name_product, sum(num_op)
from product p1
left join number n2
on n2.id_product = p1.id_product
left join
(
select imei, count(operation) as num_op
from Operation
group by imei
) x
on x.imei = n2.imei
group by name_product
对于已经进行过操作的产品数量:
select name_product, count(distinct o3.imei)
from product p1
left join number n2
on n2.id_product = p1.id_product
left join Operation o3
on o3.imei = n2.imei
group by name_product
答案 1 :(得分:0)
可能的解决方案:
SELECT name_product, COUNT(*)
FROM product p
LEFT JOIN number n ON n.id_product = p.id_product
LEFT JOIN operation o ON o.imei = n.imei
GROUP BY p.id_product
答案 2 :(得分:0)
SELECT product.id_product,COUNT(number.id_product)
FROM product
INNER JOIN number ON product.id_product = number.id_product;
GROUP BY product.id_product
答案 3 :(得分:0)
SELECT name_product, count (imei) c
FROM (SELECT name_product, o.imei
FROM (SELECT p.name_product, n.imei
FROM product p, "number" n
WHERE p.id_product = n.id_product) t
LEFT JOIN
Operatio o
ON t.imei = o.imei)
GROUP BY name_product
试试此代码