mysql选择行并计算每个连接的不同行

时间:2011-02-28 04:46:40

标签: mysql select count

表一

product  c  
==========================================
sample_1    Line
sample_1    LLk
sample_1    LLk
sample_1    LLk
sample_1    LLk
sample_2    LLk
sample_3    LLk
sample_2    LLk
sample_3    LLk
sample_2    Line

再加上这两张表

a    h        r 
==========================================
LLk  sample_1  gw
LLk  sample_2  okp
LLk  sample_3  ijof
Line xe     rr3
LLk  sample_4  gr2
Line xx2       o3

最后会是这样的..

基于从表2中搜索a = LLk

这是从表1和表2生成的表3

product  counts
============================
sample_1    4
sample_2    1
sample_3    2
sample_4    0

此问题的详细链接 http://pastebin.com/ds0iS4GU

2 个答案:

答案 0 :(得分:1)

这将为您提供您正在寻找的答案

select h, count(t1.product) from t2 LEFT JOIN t1 ON t1.c=t2.a and t1.product=t2.h where t2.a='LLk' group by t2.h

答案 1 :(得分:0)

你正在寻找这样的东西吗?

select product, count(a) 
from table_two 
inner join table_one on product=h and c=a
where a = 'LLk'
group by product