指定順序Order by
mysql 按照指定字段的指定值排序:
select * from t_user order by FIELD(id, 0, 2, 11, 1, 3, 4, 5, 6, 8, 9, 10) asc
逗號分隔查詢 FIND_IN_SET
使用FIND_IN_SET(str,sbstr)函數查詢 “,” 號(逗號)隔開的數據;
filed 字段存儲形式爲多個逗號隔開的值:1,2,3,5,6
SELECT * from t_user where FIND_IN_SET('1',filed)
日期處理 ADDTIME interval
修改時間的年月,其他保留不變
select t.created_at,ADDTIME (CONCAT("2014-02-",DAYOFMONTH(t.created_at))+interval 0 hour,time(t.created_at)) as newtime
from t_user t where t.id=1
檢查、修復表 CHECK REPAIR
-- 檢查表,獲得不是OK就是有問題需要修復。
check table t_user;
-- 修復表
REPAIR TABLE `t_user`;
分區表維護 - 不建議使用分區表
互聯網公司爲啥不使用mysql分區表:http://www.zuimoban.com/jiaocheng/mysql/8216.html
alter table t_logs partition by RANGE(id)
(PARTITION p0 VALUES less than (3000000),
PARTITION p1 VALUES less than (6000000),
PARTITION p2 VALUES less than (9000000),
PARTITION p3 VALUES less than (12000000),
PARTITION p4 VALUES less than MAXVALUE);
計算指定日期爲第幾個星期 WEEKOFYEAR
通過日期 計算是第幾個星期
select WEEKOFYEAR(ADDDATE('2012-01-29',1));-- 從星期天開始算第一天
select WEEKOFYEAR(NOW());-- 從星期一開始算第一天(其中:now() 爲計算當前日期的函數)
A表查詢結果,更新B表 update from
通過另外一個表查詢結果來更新表 update from
update b_gem gem inner join b_productlist list set gem.status='NEW',gem.storeroomid=list.storeroom where list.type=1 and list.sid=2 and list.serid=gem.serId
兩個字段合併成一個字段顯示 concat
將查詢結果中的2個字段放到合併到一個字段顯示
select concat(first_name,' ',last_name) as customerName from t_agmt_customer
獲取表結構信息 show full fields
show full fields from t_user;
查看錶的索引大小 Total Index Size
-- 將選擇 information_schema 數據庫
SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB') AS 'Total Index Size' FROM TABLES WHERE table_schema = 'mytable_test' and table_name='t_user';