-2

因此使用以下 2 个表 Employee:

mysql> select * from employee;
+----------+-------+---------+-----------+------------+--------------------------+------+--------+-----------+-----+
| Fname    | Minit | Lname   | SSN       | Bdate      | Address                  | Sex  | Salary | Superssn  | Dno |
+----------+-------+---------+-----------+------------+--------------------------+------+--------+-----------+-----+
| John     | B     | Smith   | 123456789 | 1955-01-09 | 731 Fondren, Cary, NC    | M    |  31500 | 333445555 |   5 |
| Franklin | T     | Wong    | 333445555 | 1945-12-08 | 638 Voss, Cary, NC       | M    |  42000 | 888665555 |   5 |
| Joyce    | A     | English | 453453453 | 1962-07-31 | 5631 Rice, Raleigh, NC   | F    |  26250 | 333445555 |   5 |
| Rameish  | K     | Naraya  | 666884444 | 1952-09-15 | 975 Fire Oak, Angier, NC | M    |  39900 | 333445555 |   5 |
| James    | E     | Borg    | 888665555 | 1927-11-10 | 450 Stone, Cary, NC      | M    |  55000 | NULL      |   1 |
| Jennifer | S     | Wallace | 987654321 | 1931-06-20 | 291 Berry, Garner, NC    | F    |  43000 | 888665555 |   4 |
| Ahmad    | V     | Jabbar  | 987987987 | 1959-03-29 | 980 Dallas, Cary, NC     | M    |  25000 | 987654321 |   4 |
| Alicia   | J     | Zelaya  | 999887777 | 1958-07-19 | 3321 Castle, Apex, NC    | F    |  25000 | 987654321 |   4 |
+----------+-------+---------+-----------+------------+--------------------------+------+--------+-----------+-----+
8 rows in set (0.01 sec)

部门:

mysql> select * from department
    -> ;
+---------+----------------+-----------+
| dnumber | dname          | mgrssn    |
+---------+----------------+-----------+
|       1 | Headquarters   | 888665555 |
|       2 | Development    | NULL      |
|       3 | Sales          | NULL      |
|       4 | Administration | 987654321 |
|       5 | Research       | 333445555 |
+---------+----------------+-----------+
5 rows in set (0.00 sec)

我需要使用 having 子句组合信息,哪个部门的员工的平均工资大于 33000。这是输出的副本:

+--------------+-------------+
| dname        | AVG(salary) |
+--------------+-------------+
| Headquarters |       55000 |
| Research     |       35000 |
+--------------+-------------+

好的,我到目前为止:

mysql> select d.dname, AVG(salary)
    -> from department as d, employee as e
    -> having avg(salary) > 33000
    -> group by d.dname;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to
use near 'group by d.dname' at line 4
mysql> select d.dname, AVG(salary)
    -> from department as d, employee as e
    -> having avg(salary) > 33000
    -> group by ????

我在小组中缺少什么?

4

1 回答 1

1

这和你所有的其他问题听起来就像你在做一些测试或课程。不应该是你至少尝试自己解决这些问题之一的时候吗?

select
  d.dname,
  AVG(salary)
from
  department d
  inner join employee e on e.Dno = d.dnumber
group by
  d.dname
having
  avg(salary) > 33000

首先,我使用 d 和 e 作为 Department 和 Employee 的别名。您可以在查询中的表名之后指定别名。您可以使用它们为字段名称添加前缀,但如果该字段仅存在于一个表中,则不必这样做。

让我们继续内连接。表之间存在关系。看起来 department.dnumber 包含部门编号。此外,employee.Dno 包含员工的部门编号。通过加入表格,您可以组合数据。该查询将为每个部门和所有员工返回一行。对于返回的每个员工,部门的信息都是重复的。

聚合。_ SQL 知道许多聚合函数。如果您选择一个字段或几个字段进行“分组”,您可以使用其他字段进行“聚合”。所以在这种情况下,我按部门名称分组。我之前说过会多次返回部门信息,部门的每个员工一次。现在,此步骤再次对该信息进行分组。它只返回一次部门名称,并汇总员工信息。在这种情况下,我使用 AVG 来计算平均工资,但您也可以使用它SUM(salary)来获取部门所有员工的总工资,或者COUNT(*)统计每个部门的员工人数。

于 2012-12-08T23:45:51.167 回答