MySQL查询从两个表打印数据

时间:2017-11-18 01:52:01

标签: mysql

enter image description here

我需要帮助编写MySQL查询以打印Employee表中所有部门的相应部门名称和Department的数量。

预期产出:

Executive 2
Technical 2
Production 1

3 个答案:

答案 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