MySQL COUNT和GROUP BY子查询

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (1181)

我正在尝试查找按分支和类别分组的帖子数。我没有得到计数为0的类别。

CREATE TABLE branches
    (`id` serial primary key, `name` varchar(7) unique)
;

INSERT INTO branches
    (`id`, `name`)
VALUES
    (1, 'branch1'),
    (2, 'branch2'),
    (3, 'branch3')
;


CREATE TABLE categories
    (`id` serial primary key, `category` varchar(4) unique)
;

INSERT INTO categories
    (`id`, `category`)
VALUES
    (1, 'cat1'),
    (2, 'cat2')
;


CREATE TABLE posts
    (`id` serial primary key, `branch_id` int, `category_id` int, `title` varchar(6), `created_at` varchar(10))
;

INSERT INTO posts
    (`id`, `branch_id`, `category_id`, `title`, `created_at`)
VALUES
    (1, 1, 1, 'Title1', '2017-12-14'),
    (2, 1, 2, 'Title2', '2018-01-05'),
    (3, 2, 1, 'Title3', '2018-01-10')
;

预期产出:

+---------+----------+----+----+
| branch  | category | c1 | c2 |
+---------+----------+----+----+
| branch1 | cat1     |  1 |  0 |
| branch1 | cat2     |  0 |  1 |
| branch2 | cat1     |  0 |  1 |
| branch2 | cat2     |  0 |  0 |
+---------+----------+----+----+

查询尝试:

SELECT b.name, x.c1, y.c2 FROM branches b
LEFT JOIN (
     SELECT COUNT(id) c1 FROM posts WHERE created_at < '2018-01-01'
     GROUP BY posts.branch_id, posts.category_id
) x x.branch_id = b.id
LEFT JOIN (
     SELECT COUNT(id) c2 FROM posts WHERE created_at BETWEEN '2018-01-01' AND '2018-01-31'
     GROUP BY posts.branch_id, posts.category_id
) y y.branch_id = b.id
GROUP BY b.id
提问于
用户回答回答于
SELECT DISTINCT x.branch_id
              , y.category_id
              , COALESCE(z.created_at < '2018-01-01',0) c1
              , COALESCE(z.created_at BETWEEN '2018-01-01' AND '2018-01-31',0) c2
           FROM posts x
           JOIN posts y
           LEFT
           JOIN posts z
             ON z.branch_id = x.branch_id
            AND z.category_id  = y.category_id;

http://sqlfiddle.com/#!9/8aabf2/31

用户回答回答于

看起来这可能会做你想要的。

说明:获取帖子中存在的分支的每个分支/类别的可能组合。进行条件求和以获得按日期范围和分支/类别计数。然后加入回到分公司。

SELECT b.b_id branch,
       b.category,
       COALESCE(Range_Sum.C1,0) C1,
       COALESCE(Range_Sum.C2,0) C2
  FROM ( SELECT b.id b_id,
                c.id c_id,
                c.category
           FROM branches b,
                categories c
          WHERE EXISTS
                  ( SELECT 1
                      FROM posts
                     WHERE b.id = posts.branch_id
                  )
       ) b
  LEFT  
  JOIN (SELECT p.branch_id,
               c.id c_id,
               c.category,
               SUM
                 ( CASE WHEN p.created_at < '2018-01-01' THEN 1 
                        ELSE 0
                    END
                 ) C1,
               SUM
                 ( CASE WHEN p.created_at BETWEEN '2018-01-01' AND '2018-01-31' THEN 1
                        ELSE 0
                    END
                 ) C2
          FROM posts p
         INNER
          JOIN categories c
            ON p.category_id = c.id
         GROUP
            BY p.branch_id,
               c.category,
               c.id
       ) Range_Sum
    ON b.b_id = Range_Sum.branch_id
   AND b.c_id = Range_Sum.c_id;

此外,只是编写易于阅读的查询 - 永远不要使用x和y作为别名。选择其他任何可能提供更多信息的内容。

扫码关注云+社区

领取腾讯云代金券