本文實(shí)例講述了mysql多個(gè)left join連接查詢用法。分享給大家供大家參考,具體如下:
mysql查詢時(shí)需要連接多個(gè)表時(shí),比如查詢訂單的商品表,需要查詢商品的其他信息,其他信息不在訂單的商品表,需要連接其他庫的表,但是連接的條件基本都是商品ID就可以了,先給一個(gè)錯(cuò)誤語句(查詢之間的嵌套,效率很低):
SELECT
A.order_id,
A.wid,
A.work_name,
A.supply_price,
A.sell_price,
A.total_num,
A.sell_profit,
A.sell_percent,
A.goods_id,
A.goods_name,
A.classify,
B.gb_name
FROM
(
SELECT
A.sub_order_id AS order_id,
A.photo_id AS wid,
A.photo_name AS work_name,
A.supply_price,
A.sell_price,
sum(A.num) AS total_num,
(
A.sell_price - A.supply_price
) AS sell_profit,
(
A.sell_price - A.supply_price
) / A.sell_price AS sell_percent,
A.goods_id,
A.goods_name,
B.goods_name AS classify
FROM
order_goods AS A
LEFT JOIN (
SELECT
A.goods_id,
A.parentid,
B.goods_name
FROM
test_qyg_goods.goods AS A
LEFT JOIN test_qyg_goods.goods AS B ON A.parentid = B.goods_id
) AS B ON A.goods_id = B.goods_id
WHERE
A.createtime >= '2016-09-09 00:00:00'
AND A.createtime = '2016-10-16 23:59:59'
AND FROM_UNIXTIME(
UNIX_TIMESTAMP(A.createtime),
'%Y-%m-%d'
) != '2016-09-28'
AND FROM_UNIXTIME(
UNIX_TIMESTAMP(A.createtime),
'%Y-%m-%d'
) != '2016-10-07'
GROUP BY
A.photo_id
ORDER BY
A.goods_id ASC
) AS A
LEFT JOIN (
SELECT
A.wid,
A.brand_id,
B.gb_name
FROM
test_qyg_user.buser_goods_list AS A
LEFT JOIN test_qyg_supplier.brands AS B ON A.brand_id = B.gbid
) AS B ON A.wid = B.wid
查詢結(jié)果耗時(shí)4秒多,explain分析,發(fā)現(xiàn)其中2個(gè)子查詢是全部掃描,可以使用mysql的多個(gè)left join
優(yōu)化
SELECT
A.sub_order_id,
A.photo_id AS wid,
A.photo_name AS work_name,
A.supply_price,
A.sell_price,
sum(A.num) AS total_num,
(
A.sell_price - A.supply_price
) AS sell_profit,
(
A.sell_price - A.supply_price
) / A.sell_price AS sell_percent,
A.goods_id,
A.goods_name,
B.parentid,
C.goods_name AS classify,
D.brand_id,
E.gb_name,
sum(
CASE
WHEN F.buy_type = 'yes' THEN
A.num
ELSE
0
END
) AS total_buy_num,
sum(
CASE
WHEN F.buy_type = 'yes' THEN
A.num
ELSE
0
END * A.sell_price
) AS total_buy_money,
sum(
CASE
WHEN F.buy_type = 'no' THEN
A.num
ELSE
0
END
) AS total_give_num,
sum(
CASE
WHEN F.buy_type = 'no' THEN
A.num
ELSE
0
END * A.sell_price
) AS total_give_money
FROM
order_goods AS A
LEFT JOIN test_qyg_goods.goods AS B ON A.goods_id = B.goods_id
LEFT JOIN test_qyg_goods.goods AS C ON B.parentid = C.goods_id
LEFT JOIN test_qyg_user.buser_goods_list AS D ON A.photo_id = D.wid
LEFT JOIN test_qyg_supplier.brands AS E ON D.brand_id = E.gbid
LEFT JOIN order_info_sub AS F ON A.sub_order_id = F.order_id
WHERE
A.createtime >= '2016-09-09 00:00:00'
AND A.createtime = '2016-10-16 23:59:59'
AND FROM_UNIXTIME(
UNIX_TIMESTAMP(A.createtime),
'%Y-%m-%d'
) != '2016-09-28'
AND FROM_UNIXTIME(
UNIX_TIMESTAMP(A.createtime),
'%Y-%m-%d'
) != '2016-10-07'
GROUP BY
A.photo_id
ORDER BY
A.goods_id ASC
查詢結(jié)果耗時(shí)0.04秒
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
您可能感興趣的文章:- Mysql連接join查詢原理知識點(diǎn)
- MySQL的join buffer原理
- 超詳細(xì)mysql left join,right join,inner join用法分析
- MySQL JOIN之完全用法
- MySQL優(yōu)化之使用連接(join)代替子查詢
- Mysql inner join on的用法實(shí)例(必看)
- MYSQL數(shù)據(jù)庫基礎(chǔ)之Join操作原理