热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ThinkPHP达人,一个非常复杂的SQL查询,用TP框架的链式方法如何实现?

{代码...}附件是数据库SQL备份。大家尝试看看,这应该算相当复杂的SQL了吧。客户端的运行结果数据库备份下载地址:7xiqxd.com2.z0.glb.qiniucdn.comdeliver.sql.zip
SELECT `app_plan`.*,
       `app_agreement`.*,
       `app_customer`.*,
       `app_product`.*,
       `app_product_category`.*,
       @计划总原发量:=(
select sum(`app_operation`.`send_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0) as `plan_total_send_weight`,
         @计划总实收量:=(
select sum(`app_operation`.`receive_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0) as `plan_total_receive_weight`,
         @计划总路损量:= IFNULL((
select sum(`app_operation`.`send_weight`-`app_operation`.`receive_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 2), 0) as `plan_total_lost_quantity`,
         @计划总在途量:= IFNULL((
select sum(`app_operation`.`send_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 1), 0) as `plan_onway_quantity`,
         @计划路损超出量:= IFNULL((
SELECT sum(`app_operation`.`send_weight`-`app_operation`.`receive_weight`- `app_plan`.`rational_loss`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   and `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 2
   AND(`send_weight`-`receive_weight`)> `app_plan`.`rational_loss`), 0) AS plan_over_lost, @路损超出扣款:= truncate(IFNULL((
SELECT sum((`app_operation`.`send_weight`-`app_operation`.`receive_weight`- `app_plan`.`rational_loss`) * `app_plan`.`plan_deduct_fee`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   and `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 2
   AND(`send_weight`-`receive_weight`)> `app_plan`.`rational_loss`), 0), 2) AS plan_total_punish_fee, @计划总发车数:= IFNULL((
select count(*)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0), 0) as `plan_count_send`, @计划总收车数:= IFNULL((
select count(*)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`operation_status`= 2
   AND `app_operation`.`is_del`= 0), 0) as `plan_count_receive`, @总运费:= truncate(IFNULL((
SELECT sum(`app_operation`.`receive_weight` * `app_operation`.`operation_fee`)
  FROM `app_operation`
 WHERE `app_operation`.plan_id= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0), 0), 2) as `plan_total_ship_fee`, @应付运费:= truncate(IFNULL((@总运费 - @路损超出扣款), 0), 2) AS total_invoice_ship_fee, @计划未发量:= truncate(IFNULL(`plan_total_quantity` - @计划总实收量 - @计划总在途量, 0), 2) AS `plan_total_not_quantity`
  FROM(`app_plan`)
  LEFT JOIN `app_agreement` ON `app_plan`.`agreement_id`= `app_agreement`.`agreement_id`
  LEFT JOIN `app_customer` ON `app_agreement`.`customer_id`= `app_customer`.`customer_id`
  LEFT JOIN `app_product` ON `app_agreement`.`product_id`= `app_product`.`product_id`
  LEFT JOIN `app_product_category` ON `app_product`.`category_id`= `app_product_category`.`category_id`
 WHERE `plan_status`= 1
   AND `app_plan`.`is_del`= 0

附件是数据库SQL备份。
大家尝试看看,这应该算相当复杂的SQL了吧。

客户端的运行结果

数据库备份下载地址:http://7xiqxd.com2.z0.glb.qiniucdn.com/deliver.sql.zip

回复内容:

SELECT `app_plan`.*,
       `app_agreement`.*,
       `app_customer`.*,
       `app_product`.*,
       `app_product_category`.*,
       @计划总原发量:=(
select sum(`app_operation`.`send_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0) as `plan_total_send_weight`,
         @计划总实收量:=(
select sum(`app_operation`.`receive_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0) as `plan_total_receive_weight`,
         @计划总路损量:= IFNULL((
select sum(`app_operation`.`send_weight`-`app_operation`.`receive_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 2), 0) as `plan_total_lost_quantity`,
         @计划总在途量:= IFNULL((
select sum(`app_operation`.`send_weight`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 1), 0) as `plan_onway_quantity`,
         @计划路损超出量:= IFNULL((
SELECT sum(`app_operation`.`send_weight`-`app_operation`.`receive_weight`- `app_plan`.`rational_loss`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   and `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 2
   AND(`send_weight`-`receive_weight`)> `app_plan`.`rational_loss`), 0) AS plan_over_lost, @路损超出扣款:= truncate(IFNULL((
SELECT sum((`app_operation`.`send_weight`-`app_operation`.`receive_weight`- `app_plan`.`rational_loss`) * `app_plan`.`plan_deduct_fee`)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   and `app_operation`.`is_del`= 0
   AND `app_operation`.`operation_status`= 2
   AND(`send_weight`-`receive_weight`)> `app_plan`.`rational_loss`), 0), 2) AS plan_total_punish_fee, @计划总发车数:= IFNULL((
select count(*)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0), 0) as `plan_count_send`, @计划总收车数:= IFNULL((
select count(*)
  from `app_operation`
 where `app_operation`.`plan_id`= `app_plan`.`plan_id`
   AND `app_operation`.`operation_status`= 2
   AND `app_operation`.`is_del`= 0), 0) as `plan_count_receive`, @总运费:= truncate(IFNULL((
SELECT sum(`app_operation`.`receive_weight` * `app_operation`.`operation_fee`)
  FROM `app_operation`
 WHERE `app_operation`.plan_id= `app_plan`.`plan_id`
   AND `app_operation`.`is_del`= 0), 0), 2) as `plan_total_ship_fee`, @应付运费:= truncate(IFNULL((@总运费 - @路损超出扣款), 0), 2) AS total_invoice_ship_fee, @计划未发量:= truncate(IFNULL(`plan_total_quantity` - @计划总实收量 - @计划总在途量, 0), 2) AS `plan_total_not_quantity`
  FROM(`app_plan`)
  LEFT JOIN `app_agreement` ON `app_plan`.`agreement_id`= `app_agreement`.`agreement_id`
  LEFT JOIN `app_customer` ON `app_agreement`.`customer_id`= `app_customer`.`customer_id`
  LEFT JOIN `app_product` ON `app_agreement`.`product_id`= `app_product`.`product_id`
  LEFT JOIN `app_product_category` ON `app_product`.`category_id`= `app_product_category`.`category_id`
 WHERE `plan_status`= 1
   AND `app_plan`.`is_del`= 0

附件是数据库SQL备份。
大家尝试看看,这应该算相当复杂的SQL了吧。

客户端的运行结果

数据库备份下载地址:http://7xiqxd.com2.z0.glb.qiniucdn.com/deliver.sql.zip

这么复杂干嘛还非要用链式来解决呢,你的SQL都出来了,直接SQL不就行了么。TP的Model只是为了方便使用,封装了常用的几种简单查询更新的操作,复杂的操作建议直接SQL实现。就算你绞尽脑汁想出了一个复杂的通过TP链式方法实现这个复杂操作的写法,最后TP还是要把你的操作转成SQL,你说你这不是瞎费劲么?

这么复杂的sql语句不建议转换成TP的sql语句,没有什么太大的意义。TP的链式方式最终还是会转义成sql语句来实现的,可以先写好sql,对然后用M对数据库实例化执行sql就可以了。还有如此复杂的sql中存在内外链接,这样会很大的消耗sql资源,如果数据量大的情况下,获取数据不易,建议拆分sql语句,获得基础数据后用php语言写算法,这样速度会快一些。

你要效能還是想要語句結構化?
要效能就直接SQL算出來
要語句理解就用Model完成邏輯

推荐阅读
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了游戏开发中的人工智能技术,包括定性行为和非定性行为的分类。定性行为是指特定且可预测的行为,而非定性行为则具有一定程度的不确定性。其中,追逐算法是定性行为的具体实例。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • MACElasticsearch安装步骤及验证方法
    本文介绍了MACElasticsearch的安装步骤,包括下载ZIP文件、解压到安装目录、启动服务,并提供了验证启动是否成功的方法。同时,还介绍了安装elasticsearch-head插件的方法,以便于进行查询操作。 ... [详细]
author-avatar
lexlee1979
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有