2017-08-03 71 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 | 錯誤結果 –

相關問題