根据一个查询中的类型获取所有用户计数,而不是mysql中的多个查询

时间:2018-07-11 19:39:19

标签: mysql sql

我试图编写一个MySQL查询,我只想从一个表中获取数据。结构如下:

+--------------------+------------------+------+-----+----------+----------------+
| Field              | Type             | Null | Key | Default  | Extra          |
+--------------------+------------------+------+-----+----------+----------------+
| id                 | int(10) unsigned | NO   | PRI | NULL     | auto_increment |
| name               | varchar(255)     | NO   |     | NULL     |                |
| lastname           | varchar(255)     | NO   |     | NULL     |                |
| slug               | text             | NO   |     | NULL     |                |
| gender             | varchar(225)     | YES  |     | NULL     |                |
| balance            | double(50,6)     | YES  |     | NULL     |                |
| email              | varchar(255)     | NO   |     | NULL     |                |
| password           | varchar(255)     | NO   |     | NULL     |                | 
| city               | varchar(255)     | NO   |     | NULL     |                |
| country            | varchar(255)     | NO   |     | NULL     |                |
| statename          | varchar(255)     | NO   |     | NULL     |                |
| countryname        | varchar(255)     | NO   |     | NULL     |                |
| dob                | date             | YES  |     | NULL     |                |
| membership         | varchar(255)     | YES  |     | standard |                |
| status             | varchar(255)     | NO   |     | NULL     |                |
| marital_status     | varchar(255)     | NO   |     | NULL     |                |
| description        | text             | NO   |     | NULL     |                |
| user_id            | int(10) unsigned | YES  | MUL | NULL     |                |
| active             | smallint(6)      | NO   |     | 1        |                |
| registertype       | varchar(255)     | NO   |     | unpaid   |                |
| created_at         | timestamp        | YES  |     | NULL     |                |
| updated_at         | timestamp        | YES  |     | NULL     |                              
+--------------------+------------------+------+-----+----------+----------------+

现在,我要在下面的列中选择这样的数据:

1) created_at (will be a date)
2) total_registrations (will be total count)
3) paid_member (count of total member where registertype=paid on a specific date)
4) unpaid_member (count of total member where registertype=unpaid on a specific date wise)

到目前为止,我可以通过以下查询成功获得类似结果:

select DATE(`created_at`) as created_at, count(*) as total_registrations 
from en_customers 
where DATE(`created_at`)  between date_sub(CURDATE(), interval 7 day) AND CURDATE() 
    AND registertype="paid"  
group by DATE(`created_at`);

从此查询中,我得到以下结果:

+------------+-----------+
| created_at | total_reg |
+------------+-----------+
| 2018-07-04 |         7 |
| 2018-07-05 |         7 |
| 2018-07-06 |         5 |
| 2018-07-07 |         4 |
| 2018-07-08 |         8 |
| 2018-07-09 |        13 |
| 2018-07-10 |        15 |
| 2018-07-11 |        14 |
+------------+-----------+

但是我无法使用我刚刚显示的列进行记录。

(谢谢)!

3 个答案:

答案 0 :(得分:2)

这样做:

select DATE(`created_at`) as created_at, count(*) as total_registrations 
    ,sum(case when registertype='paid' then 1 else 0 end) as paid_member
    ,sum(case when registertype='unpaid' then 1 else 0 end) as unpaid_member
from en_customers 
where DATE(`created_at`)  between date_sub(CURDATE(), interval 7 day) AND CURDATE()  
group by DATE(`created_at`);

该技术称为“条件聚合”。

请注意,我从registertype="paid"子句中删除了WHERE条件。

答案 1 :(得分:1)

CASE WHEN函数中使用COUNT

select DATE(`created_at`) as created_at,
       count(*) as total_registrations,
       COUNT(CASE WHEN registertype='paid' THEN 1 END) as paid_member,
       COUNT(CASE WHEN registertype='unpaid' THEN 1 END) as unpaid_member 
from en_customers 
where DATE(`created_at`)  between date_sub(CURDATE(), interval 7 day) AND CURDATE() 
group by DATE(`created_at`);

答案 2 :(得分:1)

您可以在过滤后的值上使用总和

select DATE(`created_at`) as created_at
      , count(*) as total_registrations 
      , sum(case when registertype='paid' then 1 else 0 end) paid_member
      , sum(case when registertype='unpaid' then 1 else 0 end) unpaid_member
from en_customers 
where DATE(`created_at`)  between date_sub(CURDATE(), interval 7 day) AND CURDATE() 

group by DATE(`created_at`);