`

Mysql相关

 
阅读更多
  1. ON DUPLICATE KEY UPDATE为Mysql特有语法,当insert已经存在的记录时,执行Update
    <insert id="mergeInto"  parameterType="list" keyProperty="permissionId" useGeneratedKeys="true">
        insert into sys_permission (permission_id,tag,summary,path,method)
        values
        <foreach collection="list" item="item" separator=",">
          (
          (select * from (select IFNULL(max(permission_id),#{item.permissionId}) from sys_permission where path=#{item.path}) as a),
          #{item.tag},#{item.summary},#{item.path},#{item.method})
        </foreach>
        ON DUPLICATE KEY UPDATE
        tag=  VALUES(tag),
        summary=  VALUES(summary),
        path=  VALUES(path),
        method=  VALUES(method)
      </insert>
     
  2. 构造DECODE函数的功能
    # 构造DECODE函数的功能
    # FIELD(str,str1,str2,str3,...)
    # 返回值为str1, str2, str3匹配的下标,str表示计算的结果,在找不到str的情况下,返回值为 0
    # ELT(N,str1,str2,str3,...)
    # 若N = 1,则返回值为  str1 ,若N = 2,则返回值为 str2,以此类推。若N 小于1或大于参数的数目,则返回值为 NULL
    # IFNULL(expr1,expr2)
    # 假如expr1 不为 NULL,则 IFNULL() 的返回值为 expr1; 否则其返回值为 expr2
    Select IFNULL(
      ELT(
         FIELD(1, 1, 2, 15, 16, 17, 18, 19)
       ,'Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen'
      )
    ,'Adult') str;
     
  3. 日期/时间转换为字符串 函数:date_format(date,format)
    select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');
     
  4. 字符串转换为日期)函数:str_to_date(str, format)
    select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30
     
  5. 两个日期相减 date1 - date2,返回天数 函数:datediff(date1,date2)
    select datediff('2008-08-08', '2008-08-01'); -- 7
     
  6. 两个日期相减 time1 - time2,返回 time 差值 函数:timediff(time1,time2)
    两个日期相减 time1 - time2,返回 time 差值 函数:timediff(time1,time2)
     
  7. 时间差函数:TIMESTAMPDIFF
    TIMESTAMPDIFF函数,有参数设置,可以精确到月(MONTH)天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND),使用起来比datediff函数更加灵活。对于比较的两个时间,时间小的放在前面,时间大的放在后面
    --相差1天
    select TIMESTAMPDIFF(DAY, '2018-03-20 23:59:00', '2015-03-22 00:00:00');
    --相差49小时
    select TIMESTAMPDIFF(HOUR, '2018-03-20 09:00:00', '2018-03-22 10:00:00');
    --相差2940分钟
    select TIMESTAMPDIFF(MINUTE, '2018-03-20 09:00:00', '2018-03-22 10:00:00');
     
  8. timediff输出转换为日,小时,分钟,秒格式   结果:4天20小时12分钟
    SELECT CONCAT(
        FLOOR(HOUR(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')) / 24), '天',
        MOD(HOUR(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')), 24), '小时',
        MINUTE(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')), '分钟')
     
  9. 查询字段(strlist)中包含(str)的结果,未查到结果返回0,反之返回查到的位置 函数:FIND_IN_SET(str,strlist)
    SELECT FIND_IN_SET('c', 'a,b,c,d')
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics