2016-07-25 108 views
0

如何选择* FROM表中的两个和count(t_type)FROM表的一个WHERE表一和二的物种都是平等的SELECT * FROM表中的两个和count(coulmn_name)FROM表一个

表一个=树

id | t_type 
~~~~~~~~~~~~ 
1 | Tree one 
2 | Tree two 
3 | Tree Three 
4 | Tree Four 
5 | Tree one 

表2 =请求

id | req_species 
~~~~~~~~~~~~ 
1 | Tree one 
2 | Tree two 
3 | Tree one 
4 | Tree two 

返回的表将具有相同的行数作为表的两个(请求),在这种情况下4排。

期望输出

species | Qunatity 
~~~~~~~~~~~~ 
Tree one | 2 
Tree two | 1 
Tree one | 2 
Tree two | 1 

回答

0

一种方法是使用相关子查询:

select r.species, 
     (select count(*) from trees t where t.species = r.species) as quantity 
from requests r; 
+0

是否可能选择*从r? –

+0

@SaiKiranVeeraneni。 。 。当然。只要说'r。*'而不是'r.species'。 –

0
SELECT COUNT(T1.t_type) AS QUALITY,T1.t_type 
FROM TABLE_1 AS T1 WHERE T1.t_type IN 
    (SELECT T2.req_species FROM TABLE_2 AS T2) 

GROUP BY T1.t_type 
相关问题