2013-01-31 45 views
-2

鑑於我有表stores_employees其中有兩列:查找商店擁有超過10名員工在SQL

store_id, employee_id 

我怎樣才能找到擁有超過10名員工的店嗎?最好的辦法是什麼?

+1

如果這個問題是你越不會有答案,但人問你什麼你嘗試過像約翰,反而容易點會得到你這個快速解答。將來嘗試發佈您至少嘗試過或閱讀過的內容。 –

+1

同意@Luis,-1從我這裏。 – nothrow

回答

4

您將在HAVING子句中使用聚合函數和GROUP BY store_id

select store_id 
from stores_employees 
group by store_id 
having count(employee_id) > 10 

SQL Fiddle with Demo

如果要包括員工總數,那麼你可以使用:

select store_id, count(employee_id) TotalEmployees 
from stores_employees 
group by store_id 
having count(employee_id) > 10 
2
SELECT store_id, count(employee_id) FROM stores_employees GROUP BY store_id 
HAVING COUNT(employee_id) > 10 
0

從邏輯上考慮問題您需要從stores_emloyees表中計數store_id和employee_id的實例,其中count> 10。

只需使用命令查看哪個顯示所需的結果。我發現這是學習mysql最有效的方法,而不是從網站複製。

關鍵字您可以使用:

SELECT 
FROM 
ORDER BY 
BETWEEN