56,687
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `work_log` (
`id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT '',
`work_date` varchar(10) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1;
insert into work_log(name,work_date) values
('1000','2012-10-11'),('1000','2012-10-12'),('1000','2012-10-13'),('1000','2012-10-14'),('1001','2012-10-11'),('1001','2012-10-12'),('1001','2012-10-13')
,('1001','2012-10-14'),('1001','2012-10-16'),('1002','2012-10-11');
select name,work_date ,count(*) as counts
from work_log
group by name,work_date
with ROLLUP;
+------+------------+--------+
| name | work_date | counts |
+------+------------+--------+
| 1000 | 2012-10-11 | 1 |
| 1000 | 2012-10-12 | 1 |
| 1000 | 2012-10-13 | 1 |
| 1000 | 2012-10-14 | 1 |
| 1000 | NULL | 4 |
| 1001 | 2012-10-11 | 1 |
| 1001 | 2012-10-12 | 1 |
| 1001 | 2012-10-13 | 1 |
| 1001 | 2012-10-14 | 1 |
| 1001 | 2012-10-16 | 1 |
| 1001 | NULL | 5 |
| 1002 | 2012-10-11 | 1 |
| 1002 | NULL | 1 |
| NULL | NULL | 10 |
+------+------------+--------+
14 rows in set (0.03 sec)
1000 | NULL | 4
1001 | NULL | 5
1002 | NULL | 1
select name,work_date ,count(*) as counts
from work_log
group by name,work_date
with ROLLUP
having isnull(name) xor isnull(work_date)=0
可以正常运行HAVING (`name` IS NOT NULL AND work_date IS NOT NULL) OR (`name` IS NULL AND work_date IS NULL)
select name,work_date ,count(*) as counts
from work_log
group by name,work_date
union
select null,null,count(*) as counts
from work_log
select name,work_date ,count(*) as counts
from work_log
group by name,work_date
having isnull(name) xor isnull(work_date)=0
with ROLLUP;