2017-11-18 111 views
0

enter image description hereMySQL查詢從兩個表中

我需要寫一個MySQL查詢打印相應部門的名稱和數量EmployeeDepartment表中的所有部門幫助打印數據。

預期輸出:

Executive 2 
Technical 2 
Production 1 

回答

1

一個join命令將是你在找什麼。

select transaction.username, transaction.transactiondate, products.price, products.quantity, products.description 
from transaction, products 
where products.productid = transaction.productid 
    and products.productid = IDHERE 
0

我假設你有一個簡單的表,其結構如下圖顯示:

+--------+------+ 
| name | dep | 
+--------+------+ 
| frank | IT | 
| jack | IT | 
| Sissel | FA | 
| Li  | FA | 
| Mok | PM | 
+--------+------+ 

你有三個部門可能更多,你可以簡單的使用count,爲各部門員工取數。如果你使用group by dep,你會得到你期望的每個數字。

SELECT dep, count(*) FROM user_table GROUP BY dep; 

然後你得到:

+------+----------+ 
| dep | count(*) | 
+------+----------+ 
| FA |  2 | 
| IT |  2 | 
| PM |  1 | 
+------+----------+ 

希望,這就是你的需要〜

0
SELECT a.name as department_name, count(b.id) as num_of_employees 
FROM department a INNER JOIN employee b ON a.dept_id = b.dept_id 
GROUP BY a.dept_id