sql数据库查询相关操作,SQL的应用——SQL多表连查、子查询、多行子查询
|
1
|
**SQL多表连查** |
|
1
2
3
4
5
6
7
8
|
--查询员工和部门信息select * from emp e,dept d where e.deptno=d.deptno--查询员工姓名,部门名称select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno--查询所有员工姓名,部门名称select e.*, d.dname from emp e,dept d where e.deptno=d.deptno--查询工资大于3000的员工姓名,工资和部门名称select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno and e.sal>=3000 |
|
1
2
3
4
|
--非等值查询--查询公司工资等级select * from emp e,salgrands where e.sal<=s.hisal and e.sal>s.losalselect * from salagrade |
|
1
2
3
4
5
|
--外链接--左外连接select*from emp e,dept d where e.deptno=d.deptno(+)--右外连接select*from emp e,dept d where e.deptno(+)=d.deptno |
|
1
2
3
4
5
6
|
--自连接--查询员工姓名和经理姓名select e1.ename ,e2.ename from emp e1,emp e2 where e1.mgr=e2.deptno--查询员工姓名、经理姓名和其他经理姓名select e1.ename,e2.ename,e3.ename from emp e1,emp e2, emp e3 where e1.mgr=e2.empno and e2.empno=e3.empno |
|
1
|
**以上为92版仅在面试出现** |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
--SQL99 表连接--交叉连接 cross joinselect *from emp e cross join dept d--自然连接 natural join select *from emp natural join dept--using select e.ename,deptno from emp e join dept d using(deptno)--on 自定义连接select *from emp e join dept d on e.deptno=d.deptnoselect *from emp e1 join emp e2 on e1.mgr=e2.empno--查询员工的姓名、经理及其经理的名字select e1.ename,e2.ename,e3.ename from emp e1 join emp e2 on e1.mgr=e2.empno join emp e3 on e2.mgr=e3.empno--SQL99 inner join 两边都合法的数据select*from emp e inner join dept d on e.deptno=d.deptno--left join 以左表为主select*from emp e left join dept d on e.deptno=d.deptno--right join 以右表为主select*from emp e right join dept d on e.deptno=d.deptno--full join 全连 取两个表的所有数据select*from emp e full join dept d on e.deptno=d.deptno----查询员工的姓名、经理及其经理的名字和部门名字select e1.ename, d.dname, e2.ename, d2.dname from emp e1 left join dept d on e1.deptno = d.deptno left join emp e2 on e2.mgr = e2.empno left join dept d2 on d2.deptno = d2.deptno |
|
1
|
**子查询** |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
--比CLARK工资高的人select SAL from emp where ename='CLARK'select *from emp where sal>(select SAL from emp where ename='CLARK')--查询工资高于平均工资的雇员姓名和工资select avg(sal) from emp select ename,sal from emp where sal>(select avg(sal) from emp )--查询同SCOTT同部门的且工资比他低的员工的名字和工资select deptno from emp where ename='SCOTT'select sal from emp where ename='SCOTT'select ename, sal from emp where deptno = (select deptno from emp where ename = 'SCOTT') and sal < (select sal from emp where ename = 'SCOTT')--查询和'SMITH','SCOTT','CLARK'同一个部门的员工姓名select distinct deptno from emp where ename in('SMITH','SCOTT','CLARK')select * from emp where deptno in (select distinct deptno from emp where ename in ('SMITH', 'SCOTT', 'CLARK'))--查询和'SMITH', 'SCOTT', 'CLARK'同一个部门并不包含他们三个的员工姓名 and ename not in ('SMITH', 'SCOTT', 'CLARK')--查询工资最高的员工名字和工资select ename,sal from emp where sal=(select max(sal)from emp )--查询职务和'SCOTT'相同但是比'SCOTT'雇佣时间早的雇员信息select * from emp where job = (select job from emp where ename = 'SCOTT') and hiredate < (select hiredate from emp where ename = 'SCOTT')--查询工资比'SCOTT'高或者雇佣时间比'SCOTT'早的雇员编号和姓名select empno,ename from emp where job = (select job from emp where ename = 'SCOTT') and hiredate < (select hiredate from emp where ename = 'SCOTT') |
|
1
|
**多行子查询** |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
--查询工资低于任何一个“CLERK”的工资的雇员信息select*from emp where sal all (select sal from emp where job = 'SALESMAN')--查询部门20中职务同部门10的雇员一样的雇员信息。select *from emp where job in (select job from emp where deptno=10)and deptno=20--查询在雇员中有哪些人是经理人select * from emp where empno in (select distinct mgr from emp where mgr is not null or mgr != '')--找出部门编号为20的所有员工中收入最高的职员select ename from emp where sal = (select max(sal) from emp where deptno = 20) and deptno = 20</any(select> |
sql数据库查询相关操作,SQL的应用——SQL多表连查、子查询、多行子查询的更多相关文章
- PHP对MySQL数据库的相关操作
一.Apache服务器的安装 <1>安装版(计算机相关专业所用软件---百度云链接下载)-直接install<2>非安装版(https://www.apachehaus.com ...
- SQL SERVER: 合并相关操作(Union,Except,Intersect)
SQL SERVER: 合并相关操作(Union,Except,Intersect) use tempdb create table tempTable1 (id int primary key id ...
- python操作mysql数据库的相关操作实例
python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...
- SQL数据库的基础操作
一,认识SQL数据库 美国Microsoft公司推出的一种关系型数据库系统.SQLServer是一个可扩展的.高性能的.为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有 ...
- SQL数据库的一些操作
--以 MySQL为例 //登陆 mysql -u root -p //创建一个名为test_lib的数据库 CREATE DATABASE test_lib //删除一个名为test_lib的数据库 ...
- SQL数据库问题 解释一下下面的代码 sql 存储过程学习
SQL数据库问题 解释一下下面的代码 2008-08-13 11:30wssqyl2000 | 分类:数据库DB | 浏览1154次 use mastergocreate proc killspid( ...
- Android下的SQLite数据库的相关操作及AndroidTestCase测试
一:创建数据库 package com.itcode.mysqlite; import android.content.Context; import android.database.sqlite. ...
- MySql数据库的相关操作
SQL(Structred Query Language)结构化查询语言:和数据库交互的语言,进行数据库管理的语言. 一.数据库的操作: 1.查询所有数据库: show databases; 2.创建 ...
- 十、K3 WISE 开发插件《SQL Profiler跟踪单据操作时产生的SQL语句》
=================================== 目录: 1.查询帐套的数据库DBID 2.配置需要跟踪数据库的DBID 3.配置跟踪参数 4.跟踪进行 5.分析跟踪语句 === ...
随机推荐
- 项目alpha冲刺-测试
作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及可视化分析平台 这个作业的 ...
- django-缓存django-redis
https://django-redis-chs.readthedocs.io/zh_CN/latest/ 安装 django-redis 最简单的方法就是用 pip : pip install dj ...
- 使用mybatis框架实现带条件查询-单条件
之前我们写的查询sql都是没有带条件的,现在来实现一个新的需求,根据输入的字符串,模糊查询用户表中的信息 UserMapper.xml UserMapper.java 与jdbc的比较: 编写测试方法 ...
- angular和datatables一起使用的时候,出现TypeError: Cannot Read Property "childNodes" Of Undefined In AngularJS
在anguglar中注入'$timeout' 然后: $timeout(function{},0,false);
- 解决bash: less: command not found
问题描述 使用less命令查找日志时,less命令未找到: 解决办法 安装less,执行如下命令: npm install -g less
- WinDbg常用命令系列---内存数据显示和对应符号显示d*s(dds、dps、dqs)
命令dds, dps, dqs显示给定范围内的内存内容.假定该内存是符号表中的一系列地址.相应的符号也会显示出来. dds [Options] [Range] dqs [Options] [Rang ...
- Xshell6和Xftp6初步使用
Xshell6和Xftp6初步使用 一.Xshell6和Xftp6介绍: Xshell6:可以在Windows界面下用来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的. Xftp6:是 ...
- art-template模板引擎高级使用
一.结合express的基本使用 // npm下载express/art-template/express-art-tempalte,并且加载 var express=require('express ...
- 洛谷 P3371 【模板】单源最短路径(弱化版) 题解
P3371 [模板]单源最短路径(弱化版) 题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出 ...
- 洛谷P2877 [USACO07NOV]防晒霜Sunscreen
题目 此题有多种贪心方法. 首先简化题意: 有几个在数轴上的区间,和几个在数轴上确定的位置的点,问用这些数目的点,最多能满足多少个区间里有点. 注意:此题跟区间选点问题不一样,每个点只能满足一个区间, ...