MYSQL count標量子查詢改left join
SELECT homepageId, userId, homepagesummary, totalviews, totalleadpercents, totalappointments, homepagestatus, linkphone, imagepath, createtime, updatetime, recommendtime, sortcode, designerimagepath, isrecommend, ( SELECT count(*) FROM homepagexxxrelation WHERE homepageid = h.homepageid AND groundingstatus = 0 ) AS totalxxx, ( SELECT count(*) FROM homepagexxxrelation WHERE homepageid = h.homepageid AND groundingstatus = 0 AND Createtime >= DATE_SUB(sysdate(), INTERVAL 7 DAY) ) AS weektotalxxx FROM designerhomepage h WHERE 1 = 1 and isrecommend=1 order by weektotalxxx DESC, homepageid DESC; 最佳化後: SELECT h.homepageId, h.userId, h.homepagesummary, h.totalviews, h.totalleadpercents, h.totalappointments, h.homepagestatus, h.linkphone, h.imagepath, h.createtime, h.updatetime, h.recommendtime, h.sortcode, h.designerimagepath, h.isrecommend, ifnull(h2.cnt2,0) AS totalscheme, ifnull(h2.cnt1,0) AS weektotalxxx FROM designerhomepage h LEFT JOIN ( SELECT Homepageid, sum(case when HP.Createtime >= DATE_SUB(sysdate(), INTERVAL 7 DAY) then 1 else 0 end) cnt1, count(*) cnt2 FROM homepagexxxrelation hp WHERE HP.groundingstatus = 0 GROUP BY Homepageid ) h2 ON h2.homepageid = h.homepageid WHERE 1 = 1 and isrecommend=1 ORDER BY weektotalxxx DESC, homepageid DESC;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29863023/viewspace-2675930/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql常用連線查詢join,left,right,crossMySqlROS
- oracle update left join查詢Oracle
- mysql + left joinMySql
- MYSQL 怎麼 LEFT JOIN 多表聯查MySql
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- mysql left join轉inner joinMySql
- sql 連線查詢例項(left join)三表連線查詢SQL
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- 【MySQL】LEFT JOIN 踩坑MySql
- MySQL優化COUNT()查詢MySql優化
- MySQL筆記-左連線的使用(left join有關聯的多表查詢)MySql筆記
- mysql 標量子查詢和現金盤程式製作非法子查詢MySql
- GreatSQL 最佳化技巧:將 MINUS 改寫為標量子查詢SQL
- [20200325]慎用標量子查詢.txt
- MySQL join連表查詢示例MySql
- mysql left join 優化學習MySql優化
- MySQL 之 LEFT JOIN 避坑指南MySql
- mysql中的left join、right join 、inner join的詳細用法MySql
- [20211220]關於標量子查詢問題.txt
- Oracle常用抓取SQL-標量子查詢等OracleSQL
- LEFT JOIN 和JOIN 多表連線
- sql:left join和join區別SQL
- SQL Server Left joinSQLServer
- 深入理解mysql之left join 使用詳解MySql
- join、inner join、left join、right join、outer join的區別
- mysql的left join和inner join的效率對比,以及如何優化MySql優化
- mysql求交集:UNION ALL合併查詢,inner join內連線查詢,IN/EXISTS子查詢MySql
- [20180626]函式與標量子查詢14.txt函式
- [20180625]函式與標量子查詢13(補充)函式
- [20180612]函式與標量子查詢10.txt函式
- [20180611]函式與標量子查詢9.txt函式
- [20180607]函式與標量子查詢8.txt函式
- [20180602]函式與標量子查詢4.txt函式
- [20180602]函式與標量子查詢3.txt函式
- 20180601]函式與標量子查詢2.txt函式
- [20211214]18c標量子查詢unnest.txt
- mysql新建表和多表查詢,關鍵字joinMySql
- Mysql-left join on後接and,和where的區別MySql