- 论坛徽章:
- 0
|
本帖最后由 龙雪刚 于 2012-05-10 09:23 编辑
- mysql> select * from user;
- +----+------+--------+---------------------+
- | id | name | amount | time |
- +----+------+--------+---------------------+
- | 1 | aa | 50 | 2012-05-10 08:48:37 |
- | 2 | bb | 50 | 2012-05-10 08:48:41 |
- | 3 | aa | 50 | 2012-05-10 08:48:46 |
- | 4 | aa | 500 | 2012-05-10 08:48:50 |
- | 5 | bb | 1000 | 2012-05-10 08:48:56 |
- | 6 | cc | 1000 | 2012-05-10 08:49:00 |
- | 7 | cc | 4000 | 2012-05-10 08:49:04 |
- | 8 | cc | 4000 | 2012-05-10 08:49:08 |
- | 9 | aa | 200 | 2012-05-10 08:49:19 |
- | 10 | dd | 200 | 2012-05-10 08:49:26 |
- | 11 | ee | 2000 | 2012-05-10 08:49:31 |
- | 12 | ee | 1000 | 2012-05-10 08:49:34 |
- | 13 | ff | 1000 | 2012-05-10 08:49:38 |
- | 14 | ff | 2000 | 2012-05-10 08:49:41 |
- | 15 | ff | 3000 | 2012-05-10 08:49:44 |
- | 16 | gg | 10000 | 2012-05-10 08:49:53 |
- +----+------+--------+---------------------+
- 16 rows in set (0.00 sec)
- mysql> select name,group_concat(amount),sum(amount) as sum,case when sum(amount) <5000 then case when sum(amount) < 1000 then "vip1" else "vip2" end else "vip3" end as level from user group by name;
- +------+----------------------+-------+-------+
- | name | group_concat(amount) | sum | level |
- +------+----------------------+-------+-------+
- | aa | 50,50,500,200 | 800 | vip1 |
- | bb | 50,1000 | 1050 | vip2 |
- | cc | 1000,4000,4000 | 9000 | vip3 |
- | dd | 200 | 200 | vip1 |
- | ee | 2000,1000 | 3000 | vip2 |
- | ff | 1000,2000,3000 | 6000 | vip3 |
- | gg | 10000 | 10000 | vip3 |
- +------+----------------------+-------+-------+
- 7 rows in set (0.00 sec)
- mysql> select level,count(*) from ( select name,case when sum(amount) <5000 then case when sum(amount) < 1000 then "vip1" else "vip2" end else "vip3" end as level from user group by name) tmp group by level;
- +-------+----------+
- | level | count(*) |
- +-------+----------+
- | vip1 | 2 |
- | vip2 | 2 |
- | vip3 | 3 |
- +-------+----------+
- 3 rows in set (0.00 sec)
复制代码 |
评分
-
查看全部评分
|