饿虎岗资源网 Design By www.oxmxm.com
本文实例讲述了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数据库计有所帮助。
饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com
暂无评论...
更新日志
2024年11月13日
2024年11月13日
- 费玉清《天之大》SONY台湾版 [WAV+CUE][1G]
- 英雄联盟双城之战第二季主题曲谁唱的 双城之战2主题曲演唱歌手介绍
- 逆水寒2024双11活动有什么 2024双11特惠活动开始时间
- 宝可梦大集结国服和国际服有什么区别 大集结国服vs国际服
- YelenaEckemoff-RomanceoftheMoon(2024)[24-WAV]
- 群星《发烧醉乐坊太湖美(民乐典范)》[WAV+CUE]
- 中国发烧名盘《中国管弦-九州之韵》DTS[WAV]
- V社官宣推出白色限量版Steam Deck!11月18日上市
- 梦回2016!《守望先锋2》限时推出6v6怀旧模式
- 吴京谈《镖人》拍摄过程:演员们打得很爽 文戏也过瘾
- 林家谦.2024-ISFP【FLAC分轨】
- 群星.1996-摇滚柔情【正大国际】【WAV+CUE】
- 王菲.2000-寓言【EMI百代】【WAV+CUE】
- 群星《妙音纪念珍藏版贰》头版限量编号24K金碟 [低速原抓WAV+CUE][1G]
- 谭咏麟-《20世纪中华歌坛名人百集珍藏版》中唱[APE+CUE][1G]