1. 程式人生 > >mysql的學習筆記(九)

mysql的學習筆記(九)

kill group by 交叉 pre 選擇 mysq user where from

mysql不支持FULL JOIN時可用UNION ALL代替

SELECT t1.user_id,t2.user_name FROM t1 LIFT JOIN t2 ON t1.user_id = t2.user_id 
UNION ALL SELECT t2.user_id,t2.user_name FROM t1 RIGHT JOIN t2 ON t1.user_id = t2.user_id

交叉連接(笛卡爾積連接)A*B,避免笛卡爾積

SELECT t1.user_id,t2.user_name FROM t2 CROSS JOIN t2

使用join優化子查詢

SELECT a.user_id,a.user_name,(SELECT user_name userName b WHERE a.user_name=b.user_name ) user_name2 FROM a
SELECT a.user_id,a.user_name FROM a LEFT JOIN b ON a.user_name = b.user_name

分組選擇

SELECT d.user_name,c.timestr,kills FROM( SELECT user_id,timestr,kills,(SELECT COUNT(*) FROM user_kills b WHERE b.user_id = a.user_id AND a.kills<=b.kills) 
AS cnt FROM user_kills a GROUP BY user_id,timestr,kills) c JOIN user1 d ON c.user_id = d.id WHERE cnt<=2

mysql的學習筆記(九)