使用 table 本身的内部连接创建 table
Create table using inner join from the table itself
我有这个 Table:
CREATE TABLE `employees` (
`EMPLOYEE_ID` decimal(6,0) NOT NULL DEFAULT '0',
`FIRST_NAME` varchar(20) DEFAULT NULL,
`LAST_NAME` varchar(25) NOT NULL,
`EMAIL` varchar(25) NOT NULL,
`PHONE_NUMBER` varchar(20) DEFAULT NULL,
`HIRE_DATE` date NOT NULL,
`JOB_ID` varchar(10) NOT NULL,
`SALARY` decimal(8,2) DEFAULT NULL,
`COMMISSION_PCT` decimal(2,2) DEFAULT NULL,
`MANAGER_ID` decimal(6,0) DEFAULT NULL,
`DEPARTMENT_ID` decimal(4,0) DEFAULT NULL,
PRIMARY KEY (`EMPLOYEE_ID`),
UNIQUE KEY `EMP_EMAIL_UK` (`EMAIL`),
KEY `EMP_DEPARTMENT_IX` (`DEPARTMENT_ID`),
KEY `EMP_JOB_IX` (`JOB_ID`),
KEY `EMP_MANAGER_IX` (`MANAGER_ID`),
KEY `EMP_NAME_IX` (`LAST_NAME`,`FIRST_NAME`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
我需要创建一个查询来查找每个 [=38] 的最低薪员工的(employee_id、LAST_NAME、FIRST_NAME、经理 ID 和薪水) =].
我尝试了很多解决方案,但 none 不起作用。
我试过:
concat(m.MANAGER_ID, ', ', min(m.salary)) as Manager,
concat(e.EMPLOYEE_ID, ', ', e.LAST_NAME, ', ', e.FIRST_NAME) as Employe
from employees as m
INNER JOIN employees as e on e.MANAGER_ID = m.MANAGER_ID and e.salary = m.salary
group by m.MANAGER_ID order by min(m.salary) desc;
那个return右边manager_id和min(salary),
但是 return 拳头 employee_id, LAST_NAME,FIRST_NAME 他发现 manager_id
我也试过:
Select e.EMPLOYEE_ID, e.LAST_NAME, e.FIRST_NAME
from employees as e
inner join (select MANAGER_ID, min(salary)
from employees
group by MANAGER_ID order by min(salary) desc) as m
on m.manager_id = e.manager_id and m.salary = e.salary;
但 return 错误代码:1054。'on clause' 中的未知列 'm.salary'
为什么它看到 e.manager_id 但未知列 'm.salary'?
提前感谢您的帮助。
您的第二次尝试非常接近。
- 您需要在子查询中为
min(salary)
提供一个列别名,这样您就可以在主 SQL. 中引用该列
- 您不想在
min(salary)
上加入,因为结果集中的每一行都是雇员的薪水,该雇员的薪水为该记录的雇员向其报告的经理赚取最少的钱。
- 您需要在主 SELECT 子句中使用
min(salary)
,以便它出现在最终结果集中。
Select e.EMPLOYEE_ID, e.LAST_NAME, e.FIRST_NAME, m.min_salary
from employees as e
inner join (
select MANAGER_ID, min(salary) as min_salary
from employees
group by MANAGER_ID
) as m
on m.manager_id = e.manager_id;
我还删除了您子查询中的 ORDER BY,因为充其量它是多余的,而在最坏的情况下它会引发错误。
对于每个人来说,这是我的新代码:
Select e.EMPLOYEE_ID, e.LAST_NAME, e.FIRST_NAME, m. MANAGER_ID, m.min_salary
from employees as e
inner join (
select MANAGER_ID, min(salary) as min_salary
from employees
group by MANAGER_ID order by min(salary) desc) as m
on m.manager_id = e.manager_id and min_salary = e.salary;
我有这个 Table:
CREATE TABLE `employees` (
`EMPLOYEE_ID` decimal(6,0) NOT NULL DEFAULT '0',
`FIRST_NAME` varchar(20) DEFAULT NULL,
`LAST_NAME` varchar(25) NOT NULL,
`EMAIL` varchar(25) NOT NULL,
`PHONE_NUMBER` varchar(20) DEFAULT NULL,
`HIRE_DATE` date NOT NULL,
`JOB_ID` varchar(10) NOT NULL,
`SALARY` decimal(8,2) DEFAULT NULL,
`COMMISSION_PCT` decimal(2,2) DEFAULT NULL,
`MANAGER_ID` decimal(6,0) DEFAULT NULL,
`DEPARTMENT_ID` decimal(4,0) DEFAULT NULL,
PRIMARY KEY (`EMPLOYEE_ID`),
UNIQUE KEY `EMP_EMAIL_UK` (`EMAIL`),
KEY `EMP_DEPARTMENT_IX` (`DEPARTMENT_ID`),
KEY `EMP_JOB_IX` (`JOB_ID`),
KEY `EMP_MANAGER_IX` (`MANAGER_ID`),
KEY `EMP_NAME_IX` (`LAST_NAME`,`FIRST_NAME`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
我需要创建一个查询来查找每个 [=38] 的最低薪员工的(employee_id、LAST_NAME、FIRST_NAME、经理 ID 和薪水) =].
我尝试了很多解决方案,但 none 不起作用。
我试过:
concat(m.MANAGER_ID, ', ', min(m.salary)) as Manager,
concat(e.EMPLOYEE_ID, ', ', e.LAST_NAME, ', ', e.FIRST_NAME) as Employe
from employees as m
INNER JOIN employees as e on e.MANAGER_ID = m.MANAGER_ID and e.salary = m.salary
group by m.MANAGER_ID order by min(m.salary) desc;
那个return右边manager_id和min(salary), 但是 return 拳头 employee_id, LAST_NAME,FIRST_NAME 他发现 manager_id
我也试过:
Select e.EMPLOYEE_ID, e.LAST_NAME, e.FIRST_NAME
from employees as e
inner join (select MANAGER_ID, min(salary)
from employees
group by MANAGER_ID order by min(salary) desc) as m
on m.manager_id = e.manager_id and m.salary = e.salary;
但 return 错误代码:1054。'on clause' 中的未知列 'm.salary' 为什么它看到 e.manager_id 但未知列 'm.salary'?
提前感谢您的帮助。
您的第二次尝试非常接近。
- 您需要在子查询中为
min(salary)
提供一个列别名,这样您就可以在主 SQL. 中引用该列
- 您不想在
min(salary)
上加入,因为结果集中的每一行都是雇员的薪水,该雇员的薪水为该记录的雇员向其报告的经理赚取最少的钱。 - 您需要在主 SELECT 子句中使用
min(salary)
,以便它出现在最终结果集中。
Select e.EMPLOYEE_ID, e.LAST_NAME, e.FIRST_NAME, m.min_salary
from employees as e
inner join (
select MANAGER_ID, min(salary) as min_salary
from employees
group by MANAGER_ID
) as m
on m.manager_id = e.manager_id;
我还删除了您子查询中的 ORDER BY,因为充其量它是多余的,而在最坏的情况下它会引发错误。
对于每个人来说,这是我的新代码:
Select e.EMPLOYEE_ID, e.LAST_NAME, e.FIRST_NAME, m. MANAGER_ID, m.min_salary
from employees as e
inner join (
select MANAGER_ID, min(salary) as min_salary
from employees
group by MANAGER_ID order by min(salary) desc) as m
on m.manager_id = e.manager_id and min_salary = e.salary;