簡體   English   中英

如何使用 sql 子查詢進行分組

[英]how to group by with a sql subqueries

我現在想不清楚,我想通過 station_id 返回計數,output 的一個例子是:

第 1 站有 3 個 fb 帖子,6 個linkedin 帖子,5 個 email 帖子 第 2 站有 3 個 fb 帖子,6 個linkedin 帖子,5 個 email 帖子

所以我需要按站號分組,我的表結構是

CREATE TABLE IF NOT EXISTS `posts` (
  `post_id` bigint(11) NOT NULL auto_increment,
  `station_id` varchar(25) NOT NULL,
  `user_id` varchar(25) NOT NULL,
  `dated` datetime NOT NULL,
  `type` enum('fb','linkedin','email') NOT NULL,
  PRIMARY KEY  (`post_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=x ;

到目前為止,我的查詢是返回站 0,因為它有一個(數據庫中的 2 個)有 2 個linkedin 帖子

SELECT Station_id, (select count(*) FROM posts WHERE type = 'linkedin') AS linkedin_count, (select count(*) FROM posts WHERE type = 'fb') AS fb_count, (select count(*) FROM posts WHERE type = 'email') AS email_count  FROM `posts` GROUP BY station_id;

或者,最快的方法是避免連接和子選擇以獲取您想要的確切格式:

SELECT
  station_id,
  SUM(CASE WHEN type = 'linkedin' THEN 1 ELSE 0 END) AS 'linkedin',
  SUM(CASE WHEN type = 'fb'       THEN 1 ELSE 0 END) AS 'fb',
  SUM(CASE WHEN type = 'email'    THEN 1 ELSE 0 END) AS 'email'
FROM posts
GROUP BY station_id;

輸出:

+------------+----------+------+-------+
| station_id | linkedin | fb   | email |
+------------+----------+------+-------+
| 1          |        3 |    2 |     5 |
| 2          |        2 |    0 |     1 |
+------------+----------+------+-------+

您可能還想在其中放置一個索引以加快速度

ALTER TABLE posts ADD INDEX (station_id, type);

解釋output:

+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key        | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
|  1 | SIMPLE      | posts | index | NULL          | station_id | 28      | NULL |   13 | Using index |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+

正如 gnif 的回答所暗示的那樣,擁有三個相關的 sub_queries 會產生性能開銷。 根據您使用的 DBMS,它的執行類似於 3 次自聯接。

gnif 的方法確保表只解析一次,而不需要連接、相關子查詢等。

gnif 的答案最明顯的缺點是你永遠不會得到 0 的記錄。 如果沒有 fb 類型,您就不會獲得記錄。 如果這不是問題,我會 go 回答他。 但是,如果這是一個問題,這里有一個與 gnif 方法相似的版本,但與您的 output 格式匹配...

SELECT
  station_id,
  SUM(CASE WHEN type = 'linkedin' THEN 1 ELSE 0 END) AS linkedin_count,
  SUM(CASE WHEN type = 'fb'       THEN 1 ELSE 0 END) AS fb_count,
  SUM(CASE WHEN type = 'email'    THEN 1 ELSE 0 END) AS email_count
FROM
  posts
GROUP BY
  station_id

給這個 go:

SELECT station_id, type, count(*) FROM posts GROUP BY station_id, type

output 格式與您嘗試獲取的格式略有不同,但它應該提供您嘗試檢索的統計信息。 此外,由於它是一個單一的查詢,它要快得多。

-- 編輯,添加示例結果集

+------------+----------+----------+
| station_id | type     | count(*) |
+------------+----------+----------+
| 1          | fb       |        2 |
| 1          | linkedin |        3 |
| 1          | email    |        5 |
| 2          | linkedin |        2 |
| 2          | email    |        1 |
+------------+----------+----------+

嘗試這個:

SELECT p.Station_id, 
(select count(*) FROM posts WHERE type = 'linkedin' and station_id=p.station_id) AS linkedin_count, 
(select count(*) FROM posts WHERE type = 'fb' and station_id=p.station_id) AS fb_count, 
(select count(*) FROM posts WHERE type = 'email' and station_id=p.station_id) AS email_count 
FROM `posts` p GROUP BY station_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM