MySql中對Group by后的結果數進行Count


今天在寫MySQ的SQL語句的時候遇到了一個奇怪的問題

1 select count(*) from subsitealbum t1, photo t2,files t3 
2           where t1.SourceAlbumId = t2.FolderId 
3           and t2.ID = t3.ID 
4           and t2.FolderId = t3.FolderID 
5           and SiteId in (select SubsiteID from channelsubsites where ChannelID='yszw') 
6     GROUP BY t1.Title,t1.Author, t3.Location,t2.CName,t2.LName,t3.Name

查詢結果如下

改為以下語句后運行正常

select count(*) count from (select count(*) from subsitealbum t1, photo t2,files t3 
          where t1.SourceAlbumId = t2.FolderId 
             and t2.ID = t3.ID 
            and t2.FolderId = t3.FolderID 
           and SiteId in (select SubsiteID from channelsubsites where ChannelID='yszw') 
          GROUP BY t1.Title,t1.Author, t3.Location,t2.CName,t2.LName,t3.Name) temp

記得臨時表一定要加別名,否則會報錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM