简体   繁体   English

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

[英]mysql select row and count each connected distinct row

table one 表一

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

Plus this table two 加表二

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

Finally will be like this.. 最终会是这样。

based on searching from table 2 where a=LLk 基于表2中的搜索,其中a = LLk

this is the table 3 which generated from table 1 and table 2 这是从表1和表2生成的表3

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

the detail link for this question http://pastebin.com/ds0iS4GU 此问题的详细链接http://pastebin.com/ds0iS4GU

这将为您提供所需的答案

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

are you looking for something like this? 您在寻找这样的东西吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM