mysql 高级语法手记

Select字段时:

CASE  WHEN  type=1  THEN  1  ELSE  0  END  as  type1

sum和count同样可以使用case then

 

时间戳转时间:

FROM_UNIXTIME(time,'%Y-%m-%d')

年月日时分秒 转 年月日:

FROM_UNIXTIME(UNIX_TIMESTAMP(time),'%Y-%m-%d')

 

计算第一个时间与第二个时间差几天

DATEDIFF('2018-11-11', '2018-11-10')   //1

DATEDIFF('2018-11-11', '2018-11-12')   //-1

DATEDIFF(CURDATE(), FROM_UNIXTIME(time/1000,'%Y-%m-%d'))  daydiff

CURDATE() //当前时间data

 

字符串操作

substring(被截取字段,从第几位开始截取,截取长度)

以下是上述语句的SQL标准版本,它更长,但更具表现力。

SUBSTRING(string FROM position FOR length);

参考:http://www.yiibai.com/mysql/substring.html

 

更新字符串的某部分

UPDATE `dede_addonarticle` SET body = REPLACE ( body, '</td>', '' ); 

 

查询解析json

JSON_EXTRACT(result,'$.data.name')

取数组第一个

json_extract(paid_info,'$data[0].paychannel')

去双引号

 JSON_UNQUOTE(json_extract(paid_info,'$data[0].paychannel'))

 

 正则查询

REGEXP

SELECT * FROM `notice` WHERE `title` REGEXP 'a|b'

 lavavel

->where('title', 'REGEXP', "a|b");

 

 MYSQL like + 变量

like concat('%',@变量,'%')

  

mysql字符大小写转换

1. LOWER()将大写转成小写

SELECT LOWER('MySql');输出mysql。

2. UPPER() 将小写转成大写

SELECT UPPER('MySql');输出MYSQL。

 

concat()函数

功能:将多个字符串连接成一个字符串。

语法:concat(str1, str2,...),返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null

举例:

select concat(area,fr,best_history_data) from test_concat order by id limit 5;

 https://www.cnblogs.com/mianbaoshu/p/11821112.html

 

MySQL 分解IP地址

select substring_index(ip,'.',1) A, substring_index(substring_index(ip,'.',2),'.',-1) B, substring_index(substring_index(ip,'.',3),'.',-1) C, substring_index(ip,'.',-1) D from (select '111.22.3.4' as ip) c

https://blog.csdn.net/u010520724/article/details/109183381

 

mysql表与表之间数据的转移

1.相同表结构

INSERT INTO table1 SELECT * FROM table2;

2.不同表结构

INSERT INTO table1(filed1,...,filedn) SELECT table2.filed1,...,table2.filedn FROM table2;

 

完整sql高阶学习:http://www.w3school.com.cn/sql/index.asp

mysql事务底层原理:https://www.cnblogs.com/rickiyang/p/13652664.html#2558627843

posted @ 2018-02-01 16:00  程序生(Codey)  阅读(543)  评论(0编辑  收藏  举报