我需要帮助编写MySQL查询以打印Employee
表中所有部门的相应部门名称和Department
的数量。
预期产出:
Executive 2
Technical 2
Production 1
答案 0 :(得分: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
答案 1 :(得分: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 |
+------+----------+
希望,这就是你的全部需要〜
答案 2 :(得分: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