您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在mysql中使用多個left join連接查詢,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
mysql查詢時需要連接多個表時,比如查詢訂單的商品表,需要查詢商品的其他信息,其他信息不在訂單的商品表,需要連接其他庫的表,但是連接的條件基本都是商品ID就可以了,先給一個錯誤語句(查詢之間的嵌套,效率很低):
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
查詢結果耗時4秒多,explain分析,發現其中2個子查詢是全部掃描,可以使用mysql的多個left join
優化
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
查詢結果耗時0.04秒
上述就是小編為大家分享的怎么在mysql中使用多個left join連接查詢了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。