SQL————高级查询
2015-07-21 20:10
459 查看
高级查询
--连接查询 select * from 表1,表2 ————形成笛卡尔积
select * from 表1,表2 where 表1.主键=表2.外键 ————主外键位置可以互换
--join on 内连接
格式: select * from 表1 join 外键 on 表1.主键 = 表2.外键
--查哪位学生的哪一门课考了多少分
select student.sname,course.cname,score.degree from student join score on score.sno=student.sno join course on course.cno = score.cno
--右连接
右边表必须显示全,如果在左边表没有与之对应的信息,则补空值
格式:select * from 表1 right join 表2 on 表1.主键 = 表2.外键
--左连接
左边表必须显示全,如果在右边表没有与之对应的信息,则补空值
select * from 表1 left join 表2 on 表1.主键 = 表2.外键
--全连接
左右两边的表都显示完全
格式:select * from 表1 full join 表2 on 表1.主键 = 表2.外键
--联合查询
对于查出的两个或多个结构相同的表联合显示用union
格式: select 列1,列2 from 表1 union select 列1,列2 from 表2
--------子查询------------------
--子查询的结果当做父查询的条件 select * from Info
--无关子查询
子查询执行是独立的,和父查询是没有关系的(没有用到父查询的东西)
select * from Info where year(Birthday)=(select YEAR(Birthday) from info where Code='p005') ————查询和学号‘p005’出生年份相同的人的信息
--相关子查询
例如:select * from teacher ————求计算机系和电子工程系不同职称的老师信息
select * from teacher t1 where depart='计算机系'
and not exists( select * from teacher t2 where depart='电子工程系' and t1.prof = t2.prof)
union
select * from teacher t1 where depart='电子工程系'
and not exists( select * from teacher t2 where depart='计算机系' and t1.prof = t2.prof)
--查询除了每门课最高分之外的其他学生信息。
select * from score
select * from score s1 where degree not in( select MAX(degree) from score s2 group by cno having s1.cno = s2.cno )
分页 select * from Car
select top 5 * from Car -————前5条数据,第一页
select top 5 * from Car where Code not in(select top 5 Code from Car) ————第二页的数据
select top 5 * from Car where Code not in(select top 10 Code from Car) ————第三页的数据
select top 5 * from Car where Code not in(select top (5*2) Code from Car) ————第三页的数据
select ceiling(COUNT(*)/5) from Car ————求总页数
select * from Car where 条件 limit 跳过几条数据,取几条数据 ————mysql里面的分页
--连接查询 select * from 表1,表2 ————形成笛卡尔积
select * from 表1,表2 where 表1.主键=表2.外键 ————主外键位置可以互换
--join on 内连接
格式: select * from 表1 join 外键 on 表1.主键 = 表2.外键
--查哪位学生的哪一门课考了多少分
select student.sname,course.cname,score.degree from student join score on score.sno=student.sno join course on course.cno = score.cno
--右连接
右边表必须显示全,如果在左边表没有与之对应的信息,则补空值
格式:select * from 表1 right join 表2 on 表1.主键 = 表2.外键
--左连接
左边表必须显示全,如果在右边表没有与之对应的信息,则补空值
select * from 表1 left join 表2 on 表1.主键 = 表2.外键
--全连接
左右两边的表都显示完全
格式:select * from 表1 full join 表2 on 表1.主键 = 表2.外键
--联合查询
对于查出的两个或多个结构相同的表联合显示用union
格式: select 列1,列2 from 表1 union select 列1,列2 from 表2
--------子查询------------------
--子查询的结果当做父查询的条件 select * from Info
--无关子查询
子查询执行是独立的,和父查询是没有关系的(没有用到父查询的东西)
select * from Info where year(Birthday)=(select YEAR(Birthday) from info where Code='p005') ————查询和学号‘p005’出生年份相同的人的信息
--相关子查询
例如:select * from teacher ————求计算机系和电子工程系不同职称的老师信息
select * from teacher t1 where depart='计算机系'
and not exists( select * from teacher t2 where depart='电子工程系' and t1.prof = t2.prof)
union
select * from teacher t1 where depart='电子工程系'
and not exists( select * from teacher t2 where depart='计算机系' and t1.prof = t2.prof)
--查询除了每门课最高分之外的其他学生信息。
select * from score
select * from score s1 where degree not in( select MAX(degree) from score s2 group by cno having s1.cno = s2.cno )
分页 select * from Car
select top 5 * from Car -————前5条数据,第一页
select top 5 * from Car where Code not in(select top 5 Code from Car) ————第二页的数据
select top 5 * from Car where Code not in(select top 10 Code from Car) ————第三页的数据
select top 5 * from Car where Code not in(select top (5*2) Code from Car) ————第三页的数据
select ceiling(COUNT(*)/5) from Car ————求总页数
select * from Car where 条件 limit 跳过几条数据,取几条数据 ————mysql里面的分页
相关文章推荐
- 7.19 SQL——函数
- Redis基本认识
- Mysql数据库笔记
- Ubuntu mysql 中文,启动,重启,远程连接相关问题
- windows下安装php5.5的redis扩展
- mysql的having语句
- sql 中的时间处理问题
- Redis能干啥?细看11种Web应用场景
- 几点建议,让Redis在你的系统中发挥更大作用
- windows程序设计——飞机大战笔记(调用登陆数据库)
- mysql分表的3种方法
- php windows 扩展redis
- 关于mysql主从复制error_code 1593的小异常BUG
- MySQL INTO OUTFILE导出导入数据|mysqldump
- MySQL INTO OUTFILE导出导入数据|mysqldump
- Oracle体系结构
- sql如何查询主键
- mysql in 子查询 效率慢 优化(转)
- mysql source 乱码
- MYSQL 修改密码