一些sql记录

1.日期加减/只保留数字

select * from HDDA_J207_WS_3_FILE a inner join( select id, (ADD_MONTHS(formatrq, formatbmqx1))as gqrq, formatrq, formatbmqx1 from ( select id, (case when formatbmqx like '%年%' then REGEXP_REPLACE(formatbmqx, '[^0-9]', '')* 12 else REGEXP_REPLACE(formatbmqx, '[^0-9]', '') end)as formatbmqx1, formatrq from ( select id, (case when BMQX isnull then '0' else BMQX end)as formatBMQX, (case length(rq) when 8 then rq else '19700101' end)as formatrq from HDDA_J207_WS_3_FILE) where formatBMQX != '永久') where gqrq<now)nt on a.id = nt.id where state = 7 and isdelete = '0' order by create_time desc

2.用一个表的字段去匹配另一个表的字段

SELECT DISTINCT yt.da_id FROM hdda yt JOIN mgck kt ON INSTR(yt.title, kt.name) > 0

3.count加条件

select b.name , count(1) as SUM, count(case bgqx when 'D10' then 1 end) as bgqx0, count(case bgqx when 'D15' then 1 end) as bgqx1, count(case bgqx when 'D30' then 1 end) as bgqx2, count(case bgqx when 'Y' then 1 end) as bgqx3, count(case bgqx when '3030' then 1 end) as bgqx4, '' as REMARK, '5' as size from hdda a left join hddacategory b on a.SUB_TABLE_NAME =b.FILETABLENAME where state = 7 group by b.name order by b.name desc

4.sql补零

--左补零
select lpad('AAA',5,'0') from dual;--00AAA
--右补零
select rpad('AAA',5,'0') from dual;--AAA00

5.行转列

select
        FUSHENPEOPLE,
        (
                select
                        wm_concat(user_name)
                from
                        sys_user
                where
                        user_id in --in外层语句
                        (
                                select DISTINCT
                                        regexp_substr(FUSHENPEOPLE //需要处理的字段, '[^,]+', 1, LEVEL) as id CONNECT BY LEVEL <= LENGTH(FUSHENPEOPLE//需要处理的字段) - LENGTH(REGEXP_REPLACE(FUSHENPEOPLE//需要处理的字段, ',', '')) + 1
                        )
        )as FUSHENPEOPLENAME
from
        BJYQ_KFJD_JOB

6.find_in_set(比行转列快很多)

 SELECT to_char(wm_concat(A.username))
        FROM JOB b JOIN (
        SELECT user_Id,user_name AS username
        FROM sys_user
        ) AS A ON FIND_IN_SET(A.user_Id,b.CHUSHENPEOPLE)>0 WHERE b.ID=a.ID

7.递归sql时,多个子节点查出来的父节点无顺序(全都加上max)

SELECT max(role_name) as role_name,max(super_id) as super_id,max(role_id) as role_id,max(type) as type,max(level_id) level_id,max(is_dept) as is_dept,max(level) as level FROM hdrole where type!=2 START WITH role_name like '%"+user+"%' CONNECT BY PRIOR super_id=role_id  group by role_id

8.sql中使用left join时,右表关联多条数据

select a.,t. from table1 a left join(
select * from (
select b.*,ROW_NUMBER() over (partition by flow_id//描述:导致出现多条数据的字段
order by TIME_ACCEPTED desc //描述:根据需要取哪个条件的数据
) as px
from table2 b)e where px =1)b on a.flow_id = b.flow_id AND b.act_type = 1 

9.SQL语句结果按照in顺序

select name,fieldname from yqhddamdatalist where type=0 AND  IS_EXPORT =1   and fieldname in('CREATOR','BGQX','WJBT')order by instr('&aposCREATOR&apos,&aposBGQX&apos,&aposWJBT&apos',CONCAT(fieldname))
//示例:
select * FromWhere id in (1,2,3,4,5) order by instr('1,2,3,4,5',CONCAT(id))
//不支持时:
DECODE (field_name , 'TITLE' , 1 , 'ND' , 2 , 'BGQX' , 3 , 'GDCS' , 4 , 'GDSJ' , 5 , 'QZH' , 6 )

你可能感兴趣的:(sql,数据库,mybatis)