创建子查询的麻烦

时间:2015-03-09 14:27:54

标签: mysql sql subquery mysql-workbench

我在使用子查询创建查询时遇到了一些麻烦。我必须使用子查询,因为它是来自学校的作业。

我有两张桌子,一张是雇员,另一张是一张。从这些表中我必须返回收入超过部门平均工资的员工。

所以这就是我到目前为止:

SELECT 
    ename,
    salary,
    (SELECT 
            department.depname
        FROM
            department
        WHERE
            department.depno = employee.depno) AS depname,
    (SELECT 
            AVG(salary)
        FROM
            employee
        WHERE
            employee.depno IN (SELECT 
                    department.depno
                FROM
                    department)) avgSalary
FROM
    employee
WHERE
    salary > (SELECT 
            AVG(salary)
        FROM
            employee
        WHERE
            employee.depno IN (SELECT 
                    department.depno
                FROM
                    department))

返回:

'JONES', '2975.00', 'RESEARCH', '2073.214286'
'BLAKE', '2850.00', 'SALES', '2073.214286'
'CLARK', '2450.00', 'ACCOUNTING', '2073.214286'
'SCOTT', '3000.00', 'RESEARCH', '2073.214286'
'KING', '5000.00', 'ACCOUNTING', '2073.214286'
'FORD', '3000.00', 'RESEARCH', '2073.214286'

但是平均工资返回所有工资的平均值。但我需要按部门这样做。

我知道如何通过执行以下查询来执行此操作:

SELECT 
    department.depname,
    (SELECT 
            AVG(salary)
        FROM
            employee
        WHERE
            department.depno = employee.depno) avgSalary
FROM
    department

返回:

ACCOUNTING  2916.666667
RESEARCH    2175.000000
SALES       1566.666667
OPERATIONS  

但我不知道如何组合它们,或者如果不使用连接就可以。

非常感谢任何帮助

编辑:

Table: employee
Columns:
empno decimal(4,0) PK 
ename varchar(10) 
efunction varchar(10) 
boss decimal(4,0) 
employed date 
salary decimal(7,2) 
commission decimal(7,2) 
depno decimal(2,0)

Table: department
Columns:
depno decimal(2,0) PK 
depname varchar(14) 
location varchar(13)

1 个答案:

答案 0 :(得分:0)

有点像这样......?

mysql> select   *
    -> from     employee e
    -> where    e.employee_salary > (
    ->             select   avg(employee_salary) avg
    ->             from     employee ee
    ->             -- this is how you join them without a join...
    ->             where    ee.department_id=e.department_id
    ->          );
+-------------+---------------+---------------+-----------------+
| employee_id | department_id | employee_name | employee_salary |
+-------------+---------------+---------------+-----------------+
|           2 |             1 | employee 2    |              40 |
|           6 |             1 | employee 6    |              50 |
+-------------+---------------+---------------+-----------------+
2 rows in set (0.04 sec)

其中....

mysql> select * from employee;
+-------------+---------------+---------------+-----------------+
| employee_id | department_id | employee_name | employee_salary |
+-------------+---------------+---------------+-----------------+
|           1 |             1 | employee 1    |              20 |
|           2 |             1 | employee 2    |              40 |
|           3 |             1 | employee 3    |              30 |
|           4 |             1 | employee 4    |              30 |
|           5 |             1 | employee 5    |              30 |
|           6 |             1 | employee 6    |              50 |
+-------------+---------------+---------------+-----------------+
6 rows in set (0.00 sec)

没有加入...

mysql> select   e.*,
    ->
    ->          (select  department_name
    ->           from    department d
    ->           where   d.department_id=e.department_id) as department_name,
    ->
    ->          (select   avg(employee_salary) avg
    ->             from     employee ee
    ->             where    ee.department_id=e.department_id) as department_average
    ->
    -> from     employee e
    ->
    -> where    e.employee_salary > (
    ->             select   avg(employee_salary) avg
    ->             from     employee ee
    ->             where    ee.department_id=e.department_id
    ->          );
+-------------+---------------+---------------+-----------------+-----------------+--------------------+
| employee_id | department_id | employee_name | employee_salary | department_name | department_average |
+-------------+---------------+---------------+-----------------+-----------------+--------------------+
|           2 |             1 | employee 2    |              40 | department 1    | 33.333333333333336 |
|           6 |             1 | employee 6    |              50 | department 1    | 33.333333333333336 |
+-------------+---------------+---------------+-----------------+-----------------+--------------------+
2 rows in set (0.00 sec)

除非真的有必要,否则你应该真的避免在select中加入,因为在现实生活中使用连接更好(为了提高数据集的效率/性能)。但where中的子查询完全合适。