我有一个看起来像这样的表:
$1 == 10000 hundredths of a cent
我需要添加一列| Date | Name | Activity |
+------------+------+----------+
| 2018-01-01 | A | 1 |
| 2018-01-01 | B | 0 |
| 2018-01-01 | C | 0 |
| 2018-02-01 | A | 0 |
| 2018-02-01 | B | 1 |
| 2018-02-01 | C | 0 |
| 2018-03-01 | A | 0 |
| 2018-03-01 | B | 0 |
| 2018-03-01 | C | 0 |
,该列基于status
列中的值显示为活动名称。
例如:
所以,我想要得到的输出是:
activity
答案 0 :(得分:-1)
MySQL 5.6模式设置:
create table user_activity(id int(11) not null auto_increment,
month date,name varchar(15),
activity enum('A','I'),primary key(id) );
insert into user_activity (month,name,activity) values('2019-01-01','alpha','A');
insert into user_activity (month,name,activity) values('2019-02-01','alpha','I');
insert into user_activity (month,name,activity) values('2019-03-01','alpha','A');
insert into user_activity (month,name,activity) values('2019-01-01','beta','A');
insert into user_activity (month,name,activity) values('2019-02-01','beta','I');
查询1 :
select * from user_activity
[结果] [2] :
| id | month | name | activity |
|----|------------|-------|----------|
| 1 | 2019-01-01 | alpha | A |
| 2 | 2019-02-01 | alpha | I |
| 3 | 2019-03-01 | alpha | A |
| 4 | 2019-01-01 | beta | A |
| 5 | 2019-02-01 | beta | I |
查询2 :
select max(month),name,activity
from user_activity
where activity='A'
group by name
[结果] [3] :
| max(month) | name | activity |
|------------|-------|----------|
| 2019-03-01 | alpha | A |
| 2019-01-01 | beta | A |