我正在尝试编写一个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条答案
按热度按时间bkhjykvo1#
可以对筛选值使用sum
wgx48brx2#
这样做:
这种技术被称为“条件聚合”。
注意,我删除了
registertype="paid"
来自WHERE
条款。iszxjhcz3#
使用
CASE WHEN
在COUNT
功能。