日期计算、正则、sequence、索引、表连接、mybatis

************************** mybatis *******************************************

#{} 的参数替换是发生在 DBMS 中,而 ${} 则发生在动态解析过程中
优先使用 #{}。因为 ${} 会导致 sql 注入的问题
表名用参数传递进来的时候,只能使用 ${}

------------------------------------- 函数-------------------------------------  
ORACLE中的支持正则表达式的函数主要有下面四个:
1,REGEXP_LIKE :与LIKE的功能相似
2,REGEXP_INSTR :与INSTR的功能相似
3,REGEXP_SUBSTR :与SUBSTR的功能相似
4,REGEXP_REPLACE :与REPLACE的功能相似
select * from tbl_user_quick_card where  regexp_like(bank_code, '^[^[:digit:]]+$')
[[:alpha:]] 任何字母。
[[:digit:]] 任何数字。
[[:alnum:]] 任何字母和数字。
[[:space:]] 任何白字符。
[[:upper:]] 任何大写字母。
[[:lower:]] 任何小写字母。
[[:punct:]] 任何标点符号。
[[:xdigit:]] 任何16进制的数字,相当于[0-9a-fA-F]

排序 :select T.* from TBL_SUPPLIER_STAGING_RATE T order by decode(T.BANK_CODE,'ABC',0,'SHB',1,'HXB',2)  --排序
连接 :select t1.*,t2.* from tbl1 t1 ,tbl2 t2 where t1.id = t2.number    相当于内连接 inner join        全连接 full join
数字函数: ROUND(3.456,2)=3.46  截断TRUNC(3.456,2)=3.45  取余 MOD(8,3)=2 绝对值 ABS(-3)=3 向下取整FLOOR(5.8)=5 向上取整 CEIL(5.4)=6
字符函数: 转大小写LOWER(x) UPPER(x) 替换 REPLACE(x,old,new) 截取 SUBSTR(x, start ,length) 拼接符号||
INSTR
instr(sourceString,destString,start,appearPosition) ('源字符串' , '目标字符串' ,'开始位置','第几次出现')
如果start的值为负数,则代表从右往左进行查找,但是位置数据仍然从左向右计算
单行函数: NVL(x,value)   decode(ps.pay_mode,'070',0,1)

字段处理后分组 : select substr(create_time, 1, 8),count(1) from tbl_channels_transaction group by substr(create_time, 1, 8)   
日期计算:select sysdate,(sysdate + interval '12' hour) from dual  --当前时间加上12小时

1,LTRIM和RTRIM 去掉查询结果左右的空格,
2,select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;
3,select to_char(sysdate,'YYYYMMDD hh24:mm:ss') from dual

--用子查询建立一个表
create table Anthor as select * from instructor where 1 = 2
--返回发现指定的字符的位置
select instr('oracle traning','ra',1,2) instring from dual
--返回字符串并将字符串的第一个字母变为大写
select initcap('smith') upp from dual
--返回一组表达式中的最大值,即比较字符的编码大小
select greatest('AA','AB','AC') from dual
select least('AA','AB','AC') from dual;
--时间点d当月份最后一天
select sysdate, LAST_DAY(sysdate) LAST_DAY from dual;  
-- NEXT_DAY(d,number) ,时间点d开始,下一个星期几的日期
--◎ 星期日 = 1  星期一 = 2  星期二 = 3  星期三 = 4  星期四 = 5  星期五 = 6  星期六 = 7
select sysdate, NEXT_DAY(sysdate,2) aa from dual;
--增加月add_months
select to_char(add_months(to_date('2016-12-29 23:23:22','yyyy-mm-dd hh24:mi:ss'),2),'yyyy-mm-dd hh24:mi:ss') from dual
--增加天
select to_char(to_date('2016-12-29 23:23:22','yyyy-mm-dd hh24:mi:ss')+62,'yyyy-mm-dd hh24:mi:ss') from dual
--增加小时、1/(24*60)增加分钟
select to_date('02-22-08 10:30:30','mm-dd-yy hh24:mi:ss') today,to_date('02-22-08 10:30:30','mm-dd-yy hh24:mi:ss')+ 1/24 next_hour from dual;
--RPAD  在列的右边粘贴字符
select rpad('gao',2,'*')from dual;
select lpad('gao',4,'*')from dual;

------------------------------------- 效率,连接-------------------------------------  
如果查询的两个表大小相当,那么用in 和exists 差别不大。 如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in
select*fromtbl_cashier_numberwherecashier_noin(selectcashier_nofromtbl_channels_transaction )
select t.* from tbl_cashier_number t where exists (select cashier_no from tbl_channels_transaction where cashier_no = t.cashier_no )

内连接:
select tbl_class c inner join tbl_student s on c.class_id = s.student_class_id
首先先将Class表和Student表进行交叉连接然后通过on后面的限制条件,只选择那些StudentClassID和ClassID相等的列
隐式内连接:
select tbl_class c,tbl_student s where c.class_id = s.student_class_id
左外链接、右外链接
全外连接:
将左边和右边表每行都至少输出一次,用关键字”full outer join”进行连接,可以看作是左外连接和右外连接的结合
自连接
------------------------------------- sequence-------------------------------------  
create sequence SEQ_CASHIER_TRANS_ID
minvalue 1
maxvalue 9999999999
start with 100000
increment by 1
cache 20
cycle;--循环,达到最大值从最小值开始循环,否则达到最大值会报错
select SQE_CASHIER_TRANS_ID.nextval from dual
drop sequence seq1

select * from all_sequences
select * from TBL_SYS_SEQUENCE   收银台表

UUID uuid = UUID.randomUUID();
String uuidStr = uuid.toString().replace( "-", "");   主键生成



CREATE UNIQUE INDEX "CARDREALUAT"."PK_TBL_USER_QUICK_CARD" ON "CARDREALUAT"."TBL_USER_QUICK_CARD" ("SERILA")
CREATE UNIQUE INDEX "CARDREALUAT"."Index_U_INFO" ON "CARDREALUAT"."TBL_USER_QUICK_CARD" ("USER_NO", "SUPPLIER_NO", "CARD_NO", "CARD_STATUS", "ACCOUNT_TYPE")
索引:创建索引可以根据查询业务的不同分为两种:单一列的索引,联合索引. 顾名思义,单一列索引就是指在表的某一列上创建索引,联合索引是在多个列上联合创建索引.
优缺点比较:
1):索引所占用空间:单一列索引相对要小.
2):索引创建时间:单一列索引相对短.
3):索引对insert,update,delete的影响程序:单一列索引要相对低.
4):在多条件查询时,联合索引效率要高.
索引的使用范围:单一列索引可以出现在where 条件中的任何位置,而联合索引需要按一定的顺序来写

条件列出现联合索引的第一列  使用联合索引
条件列没有出现联合索引的第一列  不使用联合索引
条件列在first_name和last_name中间加入另外一个条件  不使用联合索引




------------------------------------- sql-------------------------------------
分页
select t.* from (select rownum rn,tb.* from TBL_COUPON_DETAIL tb ) t  where rn > 10 and rn <= 20

------------------------------------- myBatis-------------------------------------

<select id= "queryBankParList" resultMap ="BaseResultMap">
           select
            <include refid ="Base_Column_List" />
           from TBL_SUPPLIER_STAGING_RATE where <![CDATA[ LOW_MONEY <= #{payMoney} and HIGH_MONEY >= #{payMoney}]]>
               and STATUS=#{status}
               and BANK_CODE in
        <foreach item ="item" index="index" collection= "ids" open= "(" separator ="," close=")">  
             #{item}
        </foreach >
        order by DECODE (BANK_CODE,
        <foreach item ="item" index="index" collection= "ids" open= "" separator ="," close="">  
            #{item},#{index}
        </foreach >
        ) 
   <!--按照传入的集合顺序排序-->
</select >
传入的map中ids为集合,按照集合中的顺序排列结果集
数据库sql语句  如果有变量 使用 #{}这种方式

 

<select id="queryNeedSyncTransNos" parameterType="java.util.List" resultType="java.lang.String">
        select * from
            <foreach collection="list" item="item" index="index" open="(" separator="union" close=") bb">
                select #{item,jdbcType=VARCHAR} as transNo from dual
            </foreach>        
        where not EXISTS (
            select transNo from
            (
                select transaction_no as transNo from tbl_account_transaction a where a.account_type = '020'
                union
                select sub_transaction_no as transNo from tbl_abnoral_order_handle b
            ) aa where aa.transNo = bb.transNo
        )
        
    </select>

 

<where>
                user_no = #{userNo}
            <if test="accountType != null">
                and account_type = #{accountType}
            </if>
            <if test="modifyAccountAmount != null">
                <![CDATA[ and account_amount+#{modifyAccountAmount}>=0]]>

 




 

转载于:https://www.cnblogs.com/xingminghui/p/7966068.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值