一、查詢時間的格式:sql
(1) 查詢時將時間戳格式化app
SELECT FROM_UNIXTIME(eventtime) FROM table_name函數
SELECT FROM_UNIXTIME(eventtime, '%Y-%m-%d %H:%i:%S') FROM table_namepost
(2) 查詢1月18號 post_date爲date或者datetime類型,同理能夠只 month(post_date)=n查某個月,或者 day(post_date)=n某一天,或者 year(post_date)=n 某一年的數據。ui
SELECT * FROM posts WHERE MONTH(post_date)='1' AND DAY(post_date)='18'; spa
(3)查詢datetime類型時,只對比其中的日期。例如查詢2019-01-02數據,post_date爲datetime類型、post_time爲時間戳(int)類型3d
SELECT * FROM posts WHERE DATE(post_date) = '2019-01-02'; server
SELECT * FROM posts WHERE DATE_FORMAT(post_time,'%Y-%m-%d') = '2019-01-02'; blog
二、查詢一段時間內,每5分鐘間隔分時在線數據統計(eventtime是時間戳)排序
能夠延伸統計一段時間內每10分鐘、30分鐘、1小時等時間段爲分組的登陸、付費、激活等各類數據。
SELECT FROM_UNIXTIME(`eventtime`-`eventtime`% (5*60), '%Y-%m-%d %H:%i:%S') AS stime, count(distinct uid) uids
FROM 20170828_online WHERE eventtime>=1503921000 AND eventtime<=1503925200
GROUP BY stime;
三、最高效的刪除重複記錄方法 ( 由於使用了ROWID)例子:
DELETE FROM EMP E WHERE E.ROWID > (SELECT MIN(X.ROWID) FROM EMP X WHERE X.EMP_NO = E.EMP_NO);
四、將兩個表的查詢結果合併成一行
select A.newusers,B.pay from (SELECT COUNT(DISTINCT uid) AS newusers FROM applogs.20171025_firstentry WHERE game=12 AND client=1) AS A,
(SELECT SUM(money)/100 AS pay FROM applogs.20171025_paylog WHERE eventdate=flogindate AND game=12 AND client=1 ) AS B;
五、列轉行技巧
eg:統計id爲1的記錄數,id爲2的記錄數以及id爲3的記錄數。
SELECT COUNT(CASE WHEN id=1 THEN 1 ELSE NULL END ) AS `one_num`,COUNT(CASE WHEN id=2 THEN 1 ELSE NULL END ) AS `two_num`,COUNT(CASE WHEN id=3 THEN 1 ELSE NULL END ) AS `tree_num` FROM test;
表數據:
執行該語句的結果:
六、group_concat函數的使用方法
公式:group_concat([DISTINCT] 要鏈接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
基礎表格:(以5中的表格數據爲例)
(1)以id分組,把name字段的值放在同一結果行,以逗號分隔(默認)
SELECT id,GROUP_CONCAT(NAME) AS id_name FROM test GROUP BY id;
結果:
(2)以id分組,把name字段的值放在同一結果行,以分號分隔
SELECT id,GROUP_CONCAT(NAME SEPARATOR ';') AS id_name FROM test GROUP BY id;
結果:
(3)以id分組,把去冗餘的name字段的值放在同一結果行, 以逗號分隔
SELECT id,GROUP_CONCAT(DISTINCT `name`) AS id_name FROM test GROUP BY id;
結果:
(4)以id分組,把name字段的值放在同一結果行,逗號分隔,以name排倒序
SELECT id,GROUP_CONCAT(`name` ORDER BY `name` DESC) AS id_name FROM test GROUP BY id;
結果:
七、with rollup 的用法 (表格以5中的數據表爲例)
SELECT `name`, SUM(id) FROM test.test GROUP BY NAME WITH ROLLUP
SELECT COALESCE(`name`, '總數') AS `name`, SUM(id) FROM test.test GROUP BY NAME WITH ROLLUP
結果:
COALESCE函數說明:
select coalesce(a,b,c);
若是a==null,則選擇b;若是b==null,則選擇c;若是a!=null,則選擇a;若是a b c 都爲null ,則返回爲null。coalesce(a,b,c,d...)同理。
八、插入數據出現UNIQUE索引或PRIMARY KEY衝突時就使用更新(ON DUPLICATE KEY UPDATE 語法)
(1) INSERT INTO `sy`.`day` (id, name, phone) VALUES('666', 'xst', '10086') ON DUPLICATE KEY UPDATE name = VALUES(name),phone = VALUES(phone);
(2) INSERT INTO TABLE (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; (UPDATE TABLE SET c=c+1 WHERE a=1;)
(3) INSERT INTO TABLE (a,b,c) VALUES (1,2,3),(2,5,7),(3,3,6),(4,8,2) ON DUPLICATE KEY UPDATE b=VALUES(b);
(4)插入一行數據,單主鍵已經存在,則更新該主鍵的相關數據,但數據表中 level原先的數據等於0或者爲空或者小於即將更新進去的 level 值的時候,就將新的值更新進去,不然不作處理。
INSERT IGNORE INTO test.test (uid,roleid,rolename,game,`server`,`level`) VALUES('2','00002','test2','1','1001','98') ON DUPLICATE KEY UPDATE
`level` = (CASE WHEN `level`=0 OR 'level'=NULL OR `level`<VALUES(`level`) THEN VALUES(`level`) ELSE `level` END)
九、查詢一個用戶是否連續 7天或者10天登陸(經過兩個日期字段計算其相差的天數)
SELECT eventdate FROM login WHERE user='xst' AND DATEDIFF(eventdate,firstlogindate)<=7 GROUP BY eventdate
SELECT COUNT(DISTINCT eventdate) AS days FROM login WHERE user='xst' AND DATEDIFF(eventdate,flogindate)<=7
十、查詢某個字段的一個值出現過兩次以上的記錄
(1)查詢數據表中did出現兩次以上的記錄:
select * from did_table where did in (select did from did_table group by did having count(did)>1);
(2)查詢用戶登陸數據表(記錄一個用戶登陸時的用戶和設備信息)中使用兩種以上客戶端(或者設備)登陸的用戶。uid爲用戶id,cid爲客戶端id
SELECT uid,COUNT(cid) AS cids FROM (SELECT uid,cid FROM data_base.20181104_login WHERE channel='140' GROUP BY uid,cid) AS A GROUP BY uid HAVING cids>1;
十一、將一個表(B)的某個字段數據更新到另外一個表(A)
UPDATE first_open A, open B SET A.uuid=B.uuid WHERE A.game=B.game AND A.did=B.did AND A.cid=B.cid AND A.aid=B.aid AND A.osid=B.osid and A.ip=B.ip and A.eventtime=B.eventtime
十二、查詢每一個用戶最後一條數據
select * from (select * from usertable order by eventtime desc) a group by uid;
1三、查詢兩個表某個字段的交集
(1)左表有,右表沒有
SELECT A.finish_date,B.game,A.order_num,B.pay_num,B.pay_type,A.company FROM mydb.mytable AS A LEFT JOIN mydb.testtab AS B ON A.order_num = B.pay_num WHERE B.game=63 AND B.pay_num IS NULL
(2)右表有,左表沒有
SELECT B.game,A.order_num,B.pay_num,B.pay_type FROM mydb.mytable AS A RIGHT JOIN mydb.testtab AS B ON A.order_num = B.pay_num WHERE B.game=63 AND A.order_num IS NULL
1四、sql中的三元表達式
SELECT IF(type=1,'增長','減小') AS type,role_name,item_name FROM_UNIXTIME(eventtime) FROM base.itemSELECT CASE WHEN type=1 THEN '增長' ELSE '減小' END AS type,role_name,item_name FROM_UNIXTIME(eventtime) FROM base.item