2017-08-03 87 views
-4

表A如何查询子类别

name | date   | con 
------------------------------ 
tv  | 2017-07-01 | 1 
tv  | 2017-07-02 | 1 
tv  | 2017-07-03 | 1 
tv  | 2017-07-03 | 2 
mobil | 2017-07-03 | 1 
mobil | 2017-07-04 | 1 
tablet | 2017-07-03 | 1 
tablet | 2017-07-03 | 2 
tablet | 2017-07-03 | 3 

tableB的

name  | date   | was 
------------------------------ 
tv  | 2017-07-01 | 15 
mobil | 2017-07-03 | 50 
mobil | 2017-07-04 | 15 
tablet | 2017-07-04 | 30 

结果表A(没问题):

select name, 
Group_concat(cons) as conA 
from(
select name, 
count(con) as cons 
from tableA 
where 
date between '2017-07-01' and '2017-07-31' 
GROUP BY con, name 
HAVING con>0) q 
GROUP BY name; 

如何tableA结合210 tableB? 左加入 - >我没有尝试? 子类别 - >我没有尝试? 我应该怎么办

所需的输出:

name | conA | wasB | 
------- -------- -------- 
tv  | 3,1 | 15 | 
mobil | 4  | 65 | 
tablet | 9,1,1 | 30 | 
+1

请更有可读性问题 –

回答

0

你应该subselelect与和使用count(*)的是

select q.name, 
    Group_concat(q.cons) as conA, wasB 
    from( 
    select name, 
    count(*) as cons 
    from tableA 
    where 
    date between '2017-07-01' and '2017-07-31' 
    GROUP BY con, name 
    HAVING con>0) q 
    left JOIN (
    select name, sum(was) wasB 
    from tableB 
    group by name 
) b on q.name = b.name 
    GROUP BY name 
+0

wasB假的(总) –

+0

你什么意思 ??? – scaisEdge

+0

wasB 15 | 195 | 100 | 错误结果 –

相关问题