mysq基本用法总结

工作中用到:

第一部分:拆分版本号

第一:substring_index

substring_index(str,delim,count)

      str:要处理的字符串

      delim:分隔符

      count:计数

例子:str=www.wikibt.com

      substring_index(str,'.',1)

      结果是:www

      substring_index(str,'.',2)

      结果是:www.wikibt

      也就是说,如果count是正数,那么就是从左往右数,第N个分隔符的左边的全部内容

      相反,如果是负数,那么就是从右边开始数,第N个分隔符右边的所有内容,如:

      substring_index(str,'.',-2)

      结果为:wikibt.com

     有人会问,如果我要中间的的wikibt怎么办?

     很简单的,两个方向:

      从右数第二个分隔符的右边全部,再从左数的第一个分隔符的左边:

  substring_index(substring_index(str,'.',-2),'.',1);

 

LPAD(str,len,padstr)

2=》002

返回字符串str,左填充用字符串padstr填补到len字符长度。 如果str为大于len长,返回值被缩短至len个字符(即,不能超过 len 长)。

CONCAT(
                    LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX('2.0.20.0', '.', 1), '.', -1), 3, '0'),
                    LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX('2.0.20.0', '.', 2), '.', -1), 3, '0'),
                    LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX('2.0.20.0', '.', 3), '.', -1), 3, '0'),
                    LPAD(SUBSTRING_INDEX(SUBSTRING_INDEX('2.0.20.0', '.', 4), '.', -1), 3, '0')
                )

 

第二部分:if函数

在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:

IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,

则返回expr3的值。

       其经常判断查询出来的值,示例;

 

mysql> select name,if(sex=0,'女','男') as sex from student;
+-------+-----+
| name  | sex |
+-------+-----+
| name1 | 女  |
| name2 | 女  |
| name3 | 男  |
| name4 | 女  |
+-------+-----+
4 rows in set (0.00 sec)

 

       其也经常用到判断的关联条件中,其示例如下:

 

SELECT  s.SCHOOL_CITY as schoolCity,
        s.SCHOOL_COUNTY as schoolCounty,
        count(DISTINCT `s`.`SCHOOL_ID`) as schoolNum,
        sum(m.duration) as sumDuration,
        sum(`m`.`VIEWERCOUNT`) as viewLiveSum,
        sum(m.replayViewerCount)as reViewSum,
        sum(m.praisecount) as sumpraise,
        sum(`m`.`VIEWERCOUNT`+m.replayViewerCount) as viewSum
         from ((tbl_hbb_mobile_live_statistics m join tbl_hbb_resource_visit_map v) join tbl_school_info s)
    where`m`.`RESOURCEID`= `v`.`RESOURCEID` 
        and if((`v`.`rangeTYPE`= '2'),(`v`.`rangeID`= `s`.`SCHOOL_ID`),(`v`.`parentId`= `s`.`SCHOOL_ID`))

第三部分:为空,null判断

end_version is not null and length(trim(end_version)

第四部分:MySQL中concat函数
 

使用方法:
concat(str1,str2,…)  

返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

mysql> select concat('11','22','33');
+------------------------+
| concat('11','22','33') |
+------------------------+
| 112233 |
+------------------------+
1 row in set (0.00 sec)

MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
mysql> select concat('11','22',null);
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL   |
+------------------------+
1 row in set (0.00 sec)

 

第五部分:having用法,

取出最大的一条,区别于求max

select bpal.operation_time from bi_product_action_list bpal
    left join bi_customer_product bcp on bcp.customer_id = bpal.customer_id 
    group by bpal.action
    having bpal.operation_time= max(bpal.operation_time) 
    order by bpal.operation_time
    
    select bpal.operation_time,max(bpal.operation_time) from bi_product_action_list bpal
    left join bi_customer_product bcp on bcp.customer_id = bpal.customer_id 
    group by bpal.action
    order by bpal.operation_time

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值