mysql多个left join连接查询用法分析
更新时间:2018年07月10日 12:05:11 作者:CyborgLin
这篇文章主要介绍了mysql多个left join连接查询用法,结合实例形式对比分析了mysql使用left join进行连接查询相关操作技巧与注意事项,需要的朋友可以参考下
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》
希望本文所述对大家MySQL数据库计有所帮助。
您可能感兴趣的文章:
- 图文详解Mysql使用left join写查询语句执行很慢问题的解决
- mysql使用left join连接出现重复问题的记录
- MySQL中多个left join on关联条件的顺序说明
- 关于mysql left join 查询慢时间长的踩坑总结
- MYSQL Left Join优化(10秒优化到20毫秒内)
- 解决Mysql的left join无效及使用的注意事项说明
- mysql left join快速转inner join的过程
- mysql高效查询left join和group by(加索引)
- 详解mysql 使用left join添加where条件的问题分析
- mysql中left join设置条件在on与where时的用法区别分析
- MySQL 8.0.18 Hash Join不支持left/right join左右连接问题
- MySQL left join操作中on和where放置条件的区别介绍
- MySQL利用profile分析慢sql详解(group left join效率高于子查询)
- MySQL在右表数据不唯一的情况下使用left join的方法
- MySQL表LEFT JOIN左连接与RIGHT JOIN右连接的实例教程
- mysql left join的基本用法以及on与where的区别
相关文章
关于MySQL innodb_autoinc_lock_mode介绍
下面小编就为大家带来一篇关于MySQL innodb_autoinc_lock_mode介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-03-03
最新评论