数据库
菩提树下吃烧鸡
这个作者很懒,什么都没留下…
展开
-
sql同一id有多条数据只取其中一条
同一个id有多条数据,只取其中一条需要使用:分区函数用法(partition by 字段)select *,row_number() over(partition by item order by date ) as index from tab分区索引SQL Serverselect * from (select * , row_number() over(partition by id order by state desc) as sequence from mian ) a wher原创 2021-07-21 11:55:53 · 5617 阅读 · 1 评论 -
Oracle新建索引、删除索引和查看索引
1、创建索引create index 索引名 on 表名(列名);2、删除索引drop index 索引名;3、创建组合索引create index 索引名 on 表名(列名1,列名2,列名3,…);查看目标表中已添加的索引–在数据库中查找表名select * from user_tables where table_name like ‘tablename%’;–查看该表的所有索引select * from all_indexes where table_name = ‘tablen原创 2021-07-17 10:37:12 · 732 阅读 · 0 评论 -
Oracle列转行的函数
listagg是一个列转行的函数,返回一个varchar2的字符串,measure_expr为要转换的列,可以多列,delimiter是measure_expr之间拼接的分隔符,可以没有,within group 中的order_by_caluse是measure_expr中值的排序。over后的query_partition_caluse属于分析部分,即可以对查询的结果进行分组。如:SELECT LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire原创 2021-07-16 11:59:31 · 1100 阅读 · 0 评论 -
联合索引最左前缀匹配原则
如果建立索引 index(a,b,c,d)最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到范围查询(>、<、between、like)就停止匹配,比如a = 1and b = 2 and c > 3 and d = 4如果建立(a,b,c,d)顺序的索引,d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用到,a,b,d的顺序可以任意调整。=和in可以乱序,比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mys原创 2021-07-13 15:30:00 · 201 阅读 · 0 评论 -
Oracle时间计算
24小时格式to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')YYYY-MMto_char(sysdate, 'yyyy-mm')本月第一天trunc(sysdate, 'month')指定时间当月第一天trunc(date '2017-06-05', 'month')当月最后一天trunc(last_day(sysdate))指定时间当月最后一天trunc(last_day(date '2017-06-09'))a.原创 2021-07-08 11:38:14 · 670 阅读 · 0 评论