一些sql记录

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

sql 复制代码
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.用一个表的字段去匹配另一个表的字段

sql 复制代码
SELECT DISTINCT yt.da_id FROM hdda yt JOIN mgck kt ON INSTR(yt.title, kt.name) > 0

3.count加条件

sql 复制代码
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补零

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

5.行转列

java 复制代码
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(比行转列快很多)

java 复制代码
 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)

sql 复制代码
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时,右表关联多条数据

sql 复制代码
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顺序

sql 复制代码
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 * From 表 Where 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 )
相关推荐
瓜牛_gn24 分钟前
mysql特性
数据库·mysql
奶糖趣多多1 小时前
Redis知识点
数据库·redis·缓存
数新网络2 小时前
《深入浅出Apache Spark》系列②:Spark SQL原理精髓全解析
大数据·sql·spark
CoderIsArt2 小时前
Redis的三种模式:主从模式,哨兵与集群模式
数据库·redis·缓存
师太,答应老衲吧4 小时前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
Channing Lewis5 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce
aloha_7896 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
毕业设计制作和分享7 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis
ketil277 小时前
Redis - String 字符串
数据库·redis·缓存
NiNg_1_2347 小时前
高级 SQL 技巧详解
sql