oracle-SELECT 短命女 2021-09-23 06:30 314阅读 0赞 * SELECT语法 * where子句操作符 * order by子句 * group by子句 * having子句(与group by子句一起使用,对分组的结果再一次筛选) * distinct(去重) * 算式运算符 **一:SELECT语法** select [all | distinct {* | expression | column1_name [,column2_name][,...]} from {table1_name | (subquery)}[alias] [,{table2_name | (subquery)}[alias],...] [where condition] [connect by condition [start with condition]] [group by expression [,...]] [having condition[,...]] //分别标识联合,交和差操作 [{union | intersect | minus}] [oredr by expression [asc|desc][,...]] //在查询表时将其锁住,不允许其他用户对该表进行DML操作,直到解锁为止 [for update [of [schema.] table_name | view] column][nowait]; **二:where子句操作符(那些>,<,=这些省略,只记录一部分)** <table style="width:700px;"> <thead> <tr> <th>比较操作符</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>any</td> <td>与一个列表中的任何值进行比较</td> </tr> <tr> <td>all</td> <td>与一个列表中的所有值进行比较</td> </tr> <tr> <td>between</td> <td>指定条件在两个值之间,包括边界值</td> </tr> <tr> <td>like</td> <td>匹配的字符样式,一般用于模糊查询</td> </tr> <tr> <td>in</td> <td>匹配一个列表值</td> </tr> <tr> <td>is null</td> <td>匹配空值</td> </tr> </tbody> </table> //检索id大于1,2,3任何一个值的数据行 where id>any(1,2,3); //检索id大于1,2,3三个值的数据行 where id>all(1,2,3); //闭区间[1-3] where id between 1 and 3; 或者取反:id<1或id>3 where id not between 1 and 3; //通配符_:匹配指定位置的一个字符 //通配符%:匹配从指定位置开始的任意个字符 //escape后定义的\,该\的后面的%是要搜素的字符(_和%用来进行文本匹配都是要去标识,要不就是通配符) '%\%%' escape '\' where name like 漫_云%;(第一个地方是漫,第二个是任意字符,第三个是云,第四个后面可以是任意字符) //检索id值为1,2或者3的记录 where id in(1,2,3); **三:order by子句** //可以对多个列排序,系统首先按照第一个列的值进行排序,当第一个列的值相同,再按照第二个列的值进行排序 //desc:降序;asc:升序(从上往下) select *from a order by age desc,id asc; ![20190830154924813.png][] 带有order by子句的select语句比一般的select语句需要更多时间,一般不要对查询结果进行排序 **四:group by子句** 表a,age不重复的列有四个(即12,2,15,19) ![20190830160531722.png][] select age as "年龄",count(*) as "同龄人数" from a group by age; ![20190830160819368.png][] //也可以多个列分组,Oracle首先按照第一个列分组,然后在分出来的组中按照第二列进行分组 //报错,因为id as "ID"中的id没有在group by中 select id as "ID",age as "年龄",count(*) as "同龄人数" from a group by age; //这样才是对的,首先对age进行分组,然后再在基础上细分不同的小组,所以为什么19拆开了(细分) select age as "年龄",count(*) as "同龄人数",id as "ID" from a group by age,id; ![20190830164330181.png][] **五:having子句(与group by子句一起使用,对分组的结果再一次筛选)** having子句最多可以包含40个表达式,表达式之间使用and和or隔开 having子句接受输出数据的顺序:group by>where子句>from select age as "年龄",count(*) as "同龄人数" from a group by age; ![20190830164843528.png][] **六:distinct(去重)** ![2019083016565566.png][] //对name字段重复的去掉,只取一个 select distinct name from a; ![20190830165709300.png][] **七:算式运算符** 在执行数学和日期运算时,经常使用系统提供的dual表 //数学运算 select 1+1 from dual; ![20190830170209914.png][] //日期运算 select to_date('10-1月-2019')+1 from dual; ![20190830170414494.png][] //与列关联的运算 select age+id as "id+age" from a; ![2019083017054821.png][] [20190830154924813.png]: /images/20210923/df88c9d7502f429d928957e8a524995f.png [20190830160531722.png]: /images/20210923/6f1b72b1cb7b46a8a811fe85b1636fb6.png [20190830160819368.png]: /images/20210923/6cb6ecc636f14747b5edffbb30270652.png [20190830164330181.png]: /images/20210923/9d7d28fd35964862aabe9a58fc93c938.png [20190830164843528.png]: /images/20210923/24a58557d72f46f2bee1ebeb08160df5.png [2019083016565566.png]: /images/20210923/66a2e89462ec45db90cd8dd17f70004a.png [20190830165709300.png]: /images/20210923/fd273fe138964f98979d7d5e8dda6890.png [20190830170209914.png]: /images/20210923/e186487e4da542caaa8fafc69252d3b2.png [20190830170414494.png]: /images/20210923/3761291f727c47729c951ce7d2d4c1e0.png [2019083017054821.png]: /images/20210923/31591ba7f51a4be4b983fa11b72a8974.png
还没有评论,来说两句吧...