w3resource_MySQL练习:Subquery
w3resource_MySQL练习题:Subquery
-- 要点:where里select select first_name, last_name, salary from employees where salary>( select salary from employees where last_name='Bull' )
-- 要点:直接where筛选 select first_name, last_name from employees where department_id='it'
-- 要点:多层嵌套查询 select first_name, last_name from employees where manager_id in ( -- 通过employees表获得manager_id select employee_id FROM employees where department_id in ( -- 通过departments表获得在USA的department_id select department_id from departments where location_id in ( -- 通过locations表获得在USA的location_id select location_id from locations where country_id='US' ) ) )
-- 要点:在where中传入manager_id select first_name, last_name from employees where employee_id in ( select distinct manager_id from employees )
-- 要点:where select first_name, last_name, salary from employees where salary>( select avg(salary) from employees )
-- 方法1:使用where嵌套查询 select e.first_name, e.last_name, e.salary from employees as e where e.salary=( select j.min_salary from jobs as j where j.job_id=e.job_id ) -- 方法2:使用多表连接(from连接多张表) select e.first_name, e.last_name, e.salary from employees as e, jobs as j where j.job_id=e.job_id and e.salary=j.min_salary
-- 要点:where select first_name, last_name, salary from employees where salary>(avg salary from employees) and department_id in (select distinct department_id from departments where department_name like 'IT%')
-- 要点:where select first_name, last_name, salary from employees where salary>(select salary from employees where last_name='Bell')
-- 要点:where + min() select first_name, last_name, salary from employees where salary=(select min(salary) from employees)
-- 要点:where + min() select first_name, last_name, salary from employees where salary>(select avg(salary) from employees)
-- 要点:where select first_name, last_name, salary from employees where salary>( select max(salary) from employees where job_id='SH_CLERK' ) order by salary asc
-- 要点:where + not in select first_name, last_name from employees where employee_id not in ( select manager_id from employees )
-- 1. 多表连接 select employee_id, first_name, last_name, department_name from employees, departments where employees.department_id=departments.department_id -- 2. select内筛选 SELECT employee_id, first_name, last_name, (SELECT department_name FROM departments d WHERE e.department_id = d.department_id) department FROM employees e ORDER BY department;
-- 要点:where select employee_id, first_name, last_name, salary from employees e where salary>( select avg(salary) from employees e2 where e1.department_id = e2.department_id )
-- 要点:判断奇偶数,使用%2进行取余 select * from employees where (employee_id%2<>0)
-- 1. 首先按salary进行降序排列取前五行,然后顺序排列取第一行 select * from (select * from employees order by salary desc limit 5) order by salary limit 1 -- 2. where内做筛选 SELECT DISTINCT salary FROM employees e1 WHERE 5 = ( SELECT COUNT(DISTINCT salary) FROM employees e2 WHERE e2.salary >= e1.salary );
-- 1. 首先按salary进行排列取前四行,然后顺序排列取第一行 select * from (select * from employees order by salary asc limit 4) order by salary limit 1 -- 2. where内做筛选 SELECT DISTINCT salary FROM employees e1 WHERE 4 = ( SELECT COUNT(DISTINCT salary) FROM employees e2 WHERE e2.salary >= e1.salary );
-- 要点:根据employee_id进行排序 select * from employees order by employee_id desc limit 10
-- 要点:通过department_id进行判断 select department_id, department_name from departments where department_id not in ( select distinct department_id from employees )
-- 要点:limit select * from employees order by salary desc limit 3
-- 要点:limit select * from employees order by salary asc limit 3
-- 要点:limit a, b 取数范围为:第[a+1, a+b]条记录 select * from employees order by salary limit (n-1), 1
w3resource_MySQL练习:Subquery的更多相关文章
- MySQL----This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- Oracle索引失效问题:WHERE C1='' OR C2 IN(SubQuery),并发请求时出现大量latch: cache buffers chains等待
问题描述: 项目反馈某功能响应时间很长,高峰期时系统整体响应很慢... 获取相应的AWR,问题确实比较严重,latch: cache buffers chains等待,因为这些会话SQL执行时间太长, ...
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...
- This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- DEPENDENT SUBQUERY” 和 “SUBQUERY”
http://blog.163.com/li_hx/blog/static/183991413201642410122327/ mysql> CREATE TABLE t1 (a INT, b ...
- linux之SQL语句简明教程---Subquery
我们可以在一个 SQL 语句中放入另一个 SQL 语句.当我们在 WHERE 子句或 HAVING 子句中插入另一个 SQL 语句时,我们就有一个 subquery 的架构. Subquery 的作用 ...
- cakephp , the subquery (2)
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- cakephp , the subquery
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- Subquery returns more than 1 row
Subquery returns more than 1 row表示子查询返回了多行数据 例如: select * from table1 where table1.colums=(select co ...
随机推荐
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- java类在eclipse上打jar包,Linux上成功运行的实例
1 eclipse下的java项目结构如下图所示: 2 打包的步骤如下: 3 修改minifest.mf文件: 4 .上传需要的三方jar包们和主类打的jar(案例是topV.jar)并且执行jav ...
- java wait(),notify(),notifyAll()
wait()的作用是使当前执行代码的线程进行等待,此方法是Object类的方法,该方法用来将当前线程置入“预执行队列”中,并且在wait()所带的代码处停止执行,直到接到通知或被中断位置.在调用wai ...
- 【转】ibatis 中使用select top #pagesize# * from tablename
ibatis中使用select top #num# * from tableName出现错误.由于初次用ibatis还不知道在它里边拼写SQL语句的一些规则,导致一些自认为很平常的SQL语句,在它这里 ...
- SpringMVC简介01
SpringMVC也叫Spring Web mvc,属于表现层的框架.SpringMVC是Spring框架的一部分,是在Spring3.0后发布的. Spring结构图: SpringMVC架构: S ...
- c#基础 里氏转换
1.里氏转换1).子类可以赋值给父类2).如果父类中装的是子类对象,那么可以讲这个父类强转为子类对象. 2.子类对象可以调用父类中的成员,但是父类对象永远都只能调用自己的成员. //// 1.里氏转换 ...
- 使用OpenSSH远程管理Linux服务器
一.使用OpenSSH远程管理Linux服务器 sshd是OpenSSH的服务器端守护进程,与之对应的Windows下客户端软件有SecureCRT/Xshell/PuTTY等. OpenSSH一般为 ...
- Centos离线安装Docker并加入到Swarm管理节点
以root用户登录 加入Swarm前需要在Swarm上生成Token,所以需要提前将Swarm集群搭建完成后,再运行以下命令将各虚机加入到swarm节点 下载docker离线安装包,并拷贝到/root ...
- windows系统下Eclipse启动界面更改
前段日子看到有人修改了linux系统下Eclipse的启动界面,因此自己试着修改了一下windows平台的启动界面.本文总结一下修改Eclipse 4.5(代号Mars)启动界面的方法. 方法一:修改 ...
- LINQ 基础语句
去全部集合 using (dat0216DataContext con = new dat0216DataContext()) { //LoList 是转换成 List集合 List<Us ...