1、将字元轉換為數字 cast()函數 cast(colum as unsigned)
select (1+1),('1'+'2'),(cast('1' as unsigned)+3);
2、MySQL實作行轉列
(1)group by和group_concat()函數組合使用,substring_index(group_concat(),',',N)取前N個資料
(2)as取列名為别名,再使用union all将多個查詢結果拼接,最後用order by對整體結果排序(列字段少時使用)
3、使用join更新表
update tab1 join tab2 on xx set xx where ....
4、子查詢批量插入資料
insert into tab(col1,col2...) select ...
5、活用正規表達式 regexp ^ $ . * + |
6、關聯查詢比子查詢效率快,優先使用join關聯查詢
7、if(exp,v1,v2) if()函數的使用 exp:表達式 v1:exp為真時傳回的值 v2:exp為假時傳回的值
8、case when... then... else... end case when函數可以鑲嵌使用,相對if函數靈活
9、group by比distinct性能快,考慮性能優先使用group by去重
10、coalesce()函數處理空值
(1)coalesce(col,xxx) 将空值null替換成xxx
(2)coalesce(subselect) 隻取非空的記錄
11、rollback; 事務復原
12、使用觸發器 trigger
create trigger trigname [before | after] (insert | update | delete) on for each row ......
13、對經常 group by、order by、select、distinct 的字段添加索引 index
14、使用視圖view create view viewname as select.....
15、last_insert_id()函數:查詢最後一個插入主鍵id的值
16、求集合的最大最小值:greatest()和least()函數實作