尝试一
结果:请检查是否存在语法错误或者数组越界非法访问等情况
代码:
select ee.dept_no,ee.emp_no,ss.salary from dept_emp as ee inner join salaries ss on e.emp_no =s.emp_no,
(select e.dept_no,max(s.salary) from
dept_emp as e inner join salaries s on e.emp_no =s.emp_no
where e.to_date = max(e.to_date)
group by e.dept_no ) as r
where ee.dept_no = r.dept_no and ss.salary=r.salary and ee.to_date=max(ee.to_date)
我在mysql上运行,显示下面错误:
Invalid use of group function
为什么呢?因为我的where字句之后我使用到了聚合函数
尝试二
需要注意一下,这里有一个坑。当前时间用’9999-01-01’表示。这个需要从上一道题中得出。
结果:您提交的程序没有通过所有的测试用例
代码:
select ee.dept_no,ee.emp_no,ss.salary from dept_emp as ee inner join salaries ss on ee.emp_no =ss.emp_no,
(select e.dept_no,max(s.salary) as salary from
dept_emp as e inner join salaries s on e.emp_no =s.emp_no
where e.to_date = '9999-01-01'
group by e.dept_no ) as r
where ee.dept_no = r.dept_no and ss.salary=r.salary and ee.to_date='9999-01-01'
我在msyql上查询的结果:
dept_no | emp_no | salary |
---|---|---|
d001 | 10001 | 88958 |
d004 | 10004 | 74057 |
d003 | 10005 | 94692 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d002 | 10006 | 43311 |
d005 | 10007 | 88070 |
d006 | 10009 | 70889 |
±--------±-------±-------+
存在重复的列了。难道是因为我两个表没连接,就进行where查询了。
尝试三
百度了一波(连接查询跟多表查询的区别),不是两个表没连接的原因。是我没加:
s.to_date='9999-01-01'
如果没加,相当于将同一员工前几个月的工资都查出来了。
需要注意一个坑:
虽然题目没要求排序,但是例子中排序了,所以也要排序。不然通不过
结果:答案正确:恭喜!您提交的程序通过了所有的测试用例
select ee.dept_no,ee.emp_no,ss.salary from dept_emp as ee inner join salaries ss on ee.emp_no =ss.emp_no,
(select e.dept_no,max(s.salary) as salary from
dept_emp as e inner join salaries s on e.emp_no =s.emp_no
where e.to_date = '9999-01-01' and s.to_date='9999-01-01'
group by e.dept_no ) as r
where ee.dept_no = r.dept_no and ss.salary=r.salary and ee.to_date='9999-01-01' and ss.to_date='9999-01-01'
ORDER BY ee.dept_no
总结
- where 字句后面不能加聚合函数max,sum,min等。
- 删除一张表中的所有元素。
truncate table name
- 多表查询时,不做连接,就进行查询会怎么样(连接查询跟多表查询的区别)
那将在笛卡尔集合中进行查询