SQL常用操作(一)
作者:互联网
--去重,截取,计算,替换,替换,非,范围,排序
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