2016-07-25 87 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 
相關問題