时间:2021-05-24
本文实例讲述了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数据库计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言在mysql中,多表连接查询是很常见的需求,在使用多表查询时,可以from多个表,也可以使用join连接连个表这两种查询有什么区别?哪种查询的效率更
本文实例讲述了mysql中各种常见join连表查询。分享给大家供大家参考,具体如下:通常我们需要连接多个表查询数据,以获取想要的结果。一、连接可以分为三类:
有这样的一个问题mysql查询使用mysql中left(right)join筛选条件在on与where查询出的数据是否有差异。可能只看着两个关键字看不出任何的问
本文实例讲述了tp5.1框架join方法用法。分享给大家供大家参考,具体如下:JOIN方法用于根据两个或多个表中的列之间的关系,从这些表中查询数据。join通常
mysql中using的用法为:using()用于两张表的join查询,要求using()指定的列在两个表中均存在,并使用之用于join的条件。示例:复制代码代