MySQL 两张表关联,以另一个表的值更新字段值
表A关联表B,以ID字段左关联匹配、更新表A的字段SCENIC。
UPDATE A
LEFT JOIN B
ON A.ID=B.ID
SET A.SCENIC_TYPE= B.TYPE
update student s
left join class c
on
d.index_id = c.id
set d.number =0,d.age=0
WHERE c.type='all'
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;
MySQL中的字段拼接 concat() concat_ws() group_concat()函数 - 声声慢43 - 博客园
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
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事务底层原理:一文说清 InnoDB 的事务机制 - rickiyang - 博客园