[关闭]
@3013216027 2016-01-28T13:19:26.000000Z 字数 1517 阅读 1331

关于双重NOT EXISTS的一个撕烤

数据库原理复习专栏


  1. select emp_no, first_name, last_name from employees
  2. where emp_no in (
  3. select emp_no from dept_emp group by emp_no
  4. having count(distinct dept_no)
  5. = (select count(*) from departments)
  6. );
  1. select * from departments D
  2. where not exists (
  3. select * from dept_emp R
  4. where R.dept_no = D.dept_no
  5. );
  1. select * from departments D
  2. where not exists (
  3. select * from dept_emp R
  4. where R.emp_no = x and R.dept_no = D.dept_no
  5. );
  1. select * from employees E
  2. where Z(当前从employees表取出的这个员工);
  1. select * from
  2. employees natural join dept_emp
  3. where employees.first_name = 'Annie'
  4. and dept_emp.dept_no = 'd003';

中,where后可以直接用表名.xxx来指代当前迭代到的那一个元组。所以窝们的查询语句可以写成:

  1. select * from employees E
  2. where Z(E); --此E非彼Ewhere后出现的表名均指代该表中每次迭代到的一个具体元组
  1. SELECT emp_no, CONCAT(first_name, ' ', last_name) AS name FROM employees
  2. WHERE NOT EXISTS (
  3. SELECT * FROM departments WHERE NOT EXISTS (
  4. SELECT * FROM dept_emp
  5. WHERE dept_emp.dept_no = departments.dept_no AND dept_emp.emp_no = employees.emp_no
  6. )
  7. );
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注