group by 和count 聯合使用問題

2022-05-26 15:12:09 字數 405 閱讀 6708

工作中要根據使用者發布的產品數量來排序做分頁,使用group by uid 用count(uid) 來統計的數量和想要的數量不正確。

count統計的數量是被group by 分組以後每一組中資料的數量,而不是分組的數量。

解決方法:使用子查詢

select count(1) from( 

select uid,count(uid) from test group by product

) test

裡面的查詢結果是乙個表,但是這個表沒有名字,不給他名字就報錯,所以加了乙個別名test(隨便起)

count(1)也不是很好理解就換成count(fbrandid)或count(*)

總結:有時候是在解決不了時,不妨考慮下子查詢。雖然好多人說不要用子查詢~

mysql聯合查詢時count使用的問題

select t1.id shopid t1.shop name shopname t1.logo url logourl t1.keyword keyword t1.seller id sellerid count t2.id ordercount from hx shop info t1 lef...

count 1 和count 哪個高效?

當表的資料量大些時,對錶作分析之後,使用count 1 還要比使用count 用時多了!從執行計畫來看,count 1 和count 的效果是一樣的。但是在表做過分析之後,count 1 會比count 的用時少些 1w以內資料量 不過差不了多少。如果count 1 是聚索引,id,那肯定是coun...

count 和count 1 的區別

create table test1 id number,name varchar2 50 create time date 插入1000000條資料。begin for i in 1 10000000 loop insert into test1 values i,dba fashion測試 i,...