香港云主机最佳企业级服务商!

ADSL拨号VPS包含了中国大陆(联通,移动,电信,)

中国香港,国外拨号VPS。

当前位置:云主机 > MYSQL >

电信ADSL拨号VPS
联通ADSL拨号VPS
移动ADSL拨号VPS

mysql多个left join连接查询用法分析


时间:2020-11-03 13:36 作者:admin


本文实例讲述了mysql/' target='_blank'>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_nameFROM  (    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 ALEFT 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_moneyFROM  order_goods AS ALEFT JOIN test_qyg_goods.goods AS B ON A.goods_id = B.goods_idLEFT JOIN test_qyg_goods.goods AS C ON B.parentid = C.goods_idLEFT JOIN test_qyg_user.buser_goods_list AS D ON A.photo_id = D.widLEFT JOIN test_qyg_supplier.brands AS E ON D.brand_id = E.gbidLEFT JOIN order_info_sub AS F ON A.sub_order_id = F.order_idWHERE  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_idORDER BY  A.goods_id ASC

查询结果耗时0.04秒

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》

希望本文所述对大家MySQL数据库计有所帮助。

(责任编辑:admin)






帮助中心
会员注册
找回密码
新闻中心
快捷通道
域名登录面板
虚机登录面板
云主机登录面板
关于我们
关于我们
联系我们
联系方式

售前咨询:17830004266(重庆移动)

企业QQ:383546523

《中华人民共和国工业和信息化部》 编号:ICP备00012341号

Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究

云官方微信

在线客服

  • 企业QQ: 点击这里给我发消息
  • 技术支持:383546523

  • 公司总台电话:17830004266(重庆移动)
  • 售前咨询热线:17830004266(重庆移动)