MySQL 怎樣分組查詢

來源:互聯網
上載者:User
有一個user表,表中有兩個欄位分別是username和city,表中有3條條記錄。

username city
張三 北京
張三 上海
李四 北京

如果只是查詢張三和李四各有多少人,可以通過下面語句查詢

select username,city,count(*)from usergroup by username

問:怎樣查詢在不同城市叫張三和李四的人各有多少個呢?

我希望顯示的結果是:
張三 共2人 北京1人 上海1人 ...
李四 共1人 北京1人 上海0人 ...

所以通過通過下面查詢是不可行的。這會導致出現重複行,而且也不能統計張三有多少人。

select username,city,count(*)from usergroup by username,city
張三 1張三 1李四 1

我需要的結果是:

張三 2 1 1
李四 1 1 0

==============================================================

下面是我的PHP代碼,如果單純一次查詢不能實現,那PHP應怎樣寫呢?

query($query);$num_rows = $result->num_rows;$num_cols = $result->num_fields;echo '
 
  ';for ($i=0;$i<$num_rows;$i++){$rows = $result->fetch_assoc();echo '
  
    ';foreach($rows as $cols){echo '
    ';}echo '
   ';}echo '
  
'.$cols.'
';?>

回複內容:

有一個user表,表中有兩個欄位分別是username和city,表中有3條條記錄。

username city
張三 北京
張三 上海
李四 北京

如果只是查詢張三和李四各有多少人,可以通過下面語句查詢

select username,city,count(*)from usergroup by username

問:怎樣查詢在不同城市叫張三和李四的人各有多少個呢?

我希望顯示的結果是:
張三 共2人 北京1人 上海1人 ...
李四 共1人 北京1人 上海0人 ...

所以通過通過下面查詢是不可行的。這會導致出現重複行,而且也不能統計張三有多少人。

select username,city,count(*)from usergroup by username,city
張三 1張三 1李四 1

我需要的結果是:

張三 2 1 1
李四 1 1 0

==============================================================

下面是我的PHP代碼,如果單純一次查詢不能實現,那PHP應怎樣寫呢?

query($query);$num_rows = $result->num_rows;$num_cols = $result->num_fields;echo '
 
  ';for ($i=0;$i<$num_rows;$i++){$rows = $result->fetch_assoc();echo '
  
    ';foreach($rows as $cols){echo '
    ';}echo '
   ';}echo '
  
'.$cols.'
';?>

先執行

select username, city, count(*) from user group by city, username;

搜出來的結果(簡寫):

[  0 => [    username => name1,    city => city1,    count => 1  ],  1 => [    username => name1,    city => city2,    count => 2  ],  ...]

假設結果儲存在$rs變數裡,使用php處理一下:

$user = [];foreach ($rs as $row) {    $name = $row['username'];    $city = $row['city'];    isset($user[$name]['total']) {        $user[$name]['total'] += $row['count'];    } else {        $user[$name]['total'] = $row['count'];    }    $user[$name]['detail'][$city] = $row['count'];}

當然這是按我自己喜歡的結構存的,你也可以修改成你喜歡的結構。出來的結果如下(簡寫):

[  user1 => [    total => 3,    detail => [      city1 => 1,      city2 => 2,      ...  ],  ...]

select
username,
count(*),
(select count(*) from user where username = a.username and city = '北京'),
(select count(*) from user where username = a.username and city = '上海')
from user a
group by username

如果城市很多的話,先select username,city,count(*) from user group by username,city查出每個人在每個城市中的人數,然後對結果集進行遍曆統計

select username,city,count(*)
from user
group by username

結果是:
張三 共2人 北京1人 上海1人 ...
李四 共1人 北京1人 上海0人 ...

實現代碼
select username,count(*) 共幾人,sum(case when city='北京' then 1 else 0 end) 北京,sum(case when city='上海' then 1 else 0 end) 上海 from user group by username

如何?聊天那種分組取最新資料的查詢。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.