一些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 )
相关推荐
The_Ticker19 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
Elastic 中国社区官方博客25 分钟前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
企鹅侠客30 分钟前
ETCD调优
数据库·etcd
Json_1817901448036 分钟前
电商拍立淘按图搜索API接口系列,文档说明参考
前端·数据库
煎饼小狗1 小时前
Redis五大基本类型——Zset有序集合命令详解(命令用法详解+思维导图详解)
数据库·redis·缓存
永乐春秋1 小时前
WEB-通用漏洞&SQL注入&CTF&二次&堆叠&DNS带外
数据库·sql
打鱼又晒网2 小时前
【MySQL】数据库精细化讲解:内置函数知识穿透与深度学习解析
数据库·mysql
大白要努力!2 小时前
android 使用SQLiteOpenHelper 如何优化数据库的性能
android·数据库·oracle
tatasix2 小时前
MySQL UPDATE语句执行链路解析
数据库·mysql