一些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 )
相关推荐
路在脚下@18 分钟前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
了一li2 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
码农君莫笑2 小时前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio
别致的影分身3 小时前
使用C语言连接MySQL
数据库·mysql
京东零售技术4 小时前
“慢”增长时代的企业数据体系建设:超越数据中台
数据库
sdaxue.com4 小时前
帝国CMS:如何去掉帝国CMS登录界面的认证码登录
数据库·github·网站·帝国cms·认证码
o(╥﹏╥)5 小时前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
阿里嘎多学长5 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
Yuan_o_6 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端
Sunyanhui16 小时前
牛客网 SQL36查找后排序
数据库·sql·mysql