Tips SQL SELECT @@optimizer _trace;SET optimizer_trace = 'enabled=on' ;SET optimizer_trace = 'enabled=off' ;select * from INFORMATION_SCHEMA.OPTIMIZER_TRACE;EXPLAIN < 你的 SQL > ; SHOW WARNINGS;SHOW PROCESSLIST;DESC user ;SHOW COLUMNS FROM user ;DESCRIBE user ;
视图更新 CREATE OR REPLACE VIEW 视图名 AS SELECT [...] FROM [...]
字符集转换 LEFT JOIN code_value b ON a.cost_type = CONVERT ( b.`code` USING utf8mb4 ) COLLATE utf8mb4_unicode_ci
Function select FIND_IN_SET('bb' , 'aa,bb,cc' );select FIND_IN_SET(null , '0' );COALESCE ( `code_value`.`name`, '' ) AS cost_type_name,
ON vs USING MySQL ON vs USING? | stackoverflow
删除重复数据 DELETE FROM student WHERE id NOT IN ( SELECT id FROM ( SELECT MIN ( id ) AS id FROM student GROUP BY `name` ) tmp)
要多加一层 tmp 包装,否则会遇到:1093 - You can't specify target table 'student' for update in FROM clause
备份表 create table zzz_my_table_220727 like my_table;insert into zzz_my_table_220727 select * from my_table ORDER BY id desc LIMIT 1000 ;truncate table my_table;
SQL AND OR 执行优先级 select id from table01 where condition1 or condition2 and condition3;select id from table01 where condition1 or (condition2 and condition3);select id from table01 where (condition1 or condition2) and condition3;
and
级别高于 or
。相当于可以把 and
看成 乘号 *
,把 or
看成 加号 +
。
事务 / / 对读取的记录加共享锁select ... lock in share mode;select ... for update ;
References – EOF –