ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SQL常用操作(一)

2019-09-23 20:07:41  阅读:325  来源: 互联网

标签:常用 productprice productname dual SQL 操作 rownum null select


--去重,截取,计算,替换,替换,非,范围,排序
select distinct(productid),substr(productname,1,3),productprice*1.25 productprice,orgion,
replace(productname,'器','abc'),replace(productname,'器','abc') from productinfo
where productprice <> 100 and productprice between 1000 and 3000 order by orgion nulls first

--模糊查询
select * from productinfo where productname like '%_器%' or productname like '%美'

--分组,平均,统计数量,累加
select category,avg(productprice),count(1),sum(productprice) from productinfo group by category having avg(productprice) >2000

--日期及计算
select to_char(sysdate,'YYYY-MM-DD hh;mm:ss') from dual
select add_months(to_date('2019-07-18','YYYY-MM-DD'),1) FROM DUAL

--截取   1,从字符的下标开始截取到最后,2,从字符的下标开始截取,第三个为要截取的字符长度
select substr('field',-2),substr('field',1,2) from dual;
--拼接
select '11'|| '--' || '22',concat('11','22','33') from dual;
--选择
select case '1' when '1' then '001' else  '222' end as dealNum from dual;
select decode('bb','aa','11','bb','22','33') from dual;

--空值替换
select nvl('',0) from dual;
select coalesce(null,1,null,3) from dual;

--最大最小
select greatest(1,3,5) from dual;
select least(3,2,5) from dual;

--特殊的null
    null不支持加减乘除,且null是个特殊的存在,扰乱计算规则和函数的使用
    field is null

--行和下标
select rownum , a.* from table_a a where rownum = 1 / rownum <= 2

--随机抽取
select * from (
    select productid,productname,dbms_random.value from productinfo order by dbms_random.value
) t where rownum <=3

--排序
select a,b,c,d,e,f,g from table_a order by g nulls last/first;
select a,b,c,d,e,f,g from table_a order by case b when '2' then '1' else '2' end,3
select 1 from dual where rownum ='1'
select rownum,t.* from dual

 

 

 

 

标签:常用,productprice,productname,dual,SQL,操作,rownum,null,select
来源: https://blog.csdn.net/BearSilencer/article/details/101013280

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有