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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 用LEFT JOIN優化標量子查詢優化
- 用WITH…AS改寫標量子查詢
- oracle update left join查詢Oracle
- MySql的join(連線)查詢 (三表 left join 寫法)MySql
- mysql常用連線查詢join,left,right,crossMySqlROS
- 連線查詢簡析 join 、 left join 、 right join
- 帶彙總的標量子查詢改寫
- SQL中聯表查詢操作(LEFT JOIN, RIGHT JOIN, INNER JOIN)SQL
- 標量子查詢
- mysql + left joinMySql
- MYSQL 怎麼 LEFT JOIN 多表聯查MySql
- mysql left join轉inner joinMySql
- 標量子查詢(二)
- 標量子查詢(一)
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- 三表關聯查詢-多次LEFT JOIN...ON
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- mysql中多個left join子查詢寫法以及別名用法MySql
- sql 連線查詢例項(left join)三表連線查詢SQL
- 【MySQL】LEFT JOIN 踩坑MySql
- MySQL優化COUNT()查詢MySql優化
- mysql 標量子查詢和現金盤程式製作非法子查詢MySql
- GreatSQL 最佳化技巧:將 MINUS 改寫為標量子查詢SQL
- MySQL筆記-左連線的使用(left join有關聯的多表查詢)MySql筆記
- 都是標量子查詢惹的禍
- MySQL join連表查詢示例MySql
- MySQL 之 LEFT JOIN 避坑指南MySql
- mysql left join 優化學習MySql優化
- mysql中的left join、right join 、inner join的詳細用法MySql
- Oracle常用抓取SQL-標量子查詢等OracleSQL
- 標量子查詢優化(用group by 代替distinct)優化
- mysql update join優化update in查詢效率MySql優化
- SQL Server Left joinSQLServer
- LEFT JOIN 和JOIN 多表連線
- sql:left join和join區別SQL
- 提高count查詢速度
- sql中的join、left join、right joinSQL
- 深入理解mysql之left join 使用詳解MySql