复杂的sql
程序员文章站
2022-05-01 19:55:37
复杂的sql通过前面的文章你可能觉得sql有点简单,接下来我们再来了解一些sql的其他的用法AS别名在一个复杂的sql中往往有着很多表名、列名,有时也需要自定义一下查询结果的列名此时就可以用到as实例1select * from student as a ;实例2select id as '序号',name as '姓名',sex as '性别' from student as a ;实例3select id as '序号',name as '姓名',sex as '性别' from...
复杂的sql
通过前面的文章你可能觉得sql有点简单,接下来我们再来了解一些sql的其他的用法
AS别名
在一个复杂的sql中往往有着很多表名、列名,有时也需要自定义一下查询结果的列名此时就可以用到as
实例1
select * from student as a ;
实例2
select id as '序号',name as '姓名',sex as '性别' from student as a ;
实例3
select id as '序号',name as '姓名',sex as '性别' from student as a where a.id>1;
join连接
join连接有四种
inner join、 left join 、right join、 full join
inner join
可以理解为交集;
full join
可以理解为并集;
left join
在交集的基础上加上左表;
right join
在交集的基础上加上右表;
union合并
union用来合并多个select查询的结果,注意列的个数、名称、顺序得一致,union会去掉重复的值,union all 不会。
实例1
select id from student union select id from student ;
实例2
select id from student union all select id from student ;
实例3
select id from student union select id from student order by id asc;
实例4
select id from student union all select id from student order by id asc;
结果进行了排序
实例5
select id from student order by id asc union all select id from student ;
报错
实例6
(select id from student order by id) asc union all select id from student ;
在实例5的基础上加上括号不报错,但是没有排序????仿佛order by id
没有执行。
limit 、rownum、select top 返回条数
limit用于mysql ,rownum用于oracle,select top用于Microsoft SQL Server
实例1
select id from student order by id asc limit 2 ;
实例2
select id from student order by id asc limit 1, 2 ;
表示返回2,3行数据,第一个参数表示偏移量(默认为0),第二个参数表示记录行数。
实例3
select id from student where rownum<= 2 ;
实例4
select top 5 id from student order by id asc ;
本文地址:https://blog.csdn.net/xuexilangren1/article/details/108972529
上一篇: Oracle-dql语句的执行顺序问题