时间段统计。。返回零值

xman1231 2008-02-21 08:20:29
加精

2008-01-01 A
2008-01-01 B
2008-01-02 C
2008-01-04 D
时间段
2008-01-01-2008-01-04
返回
2008-01-01 2
2008-01-02 1
2008-01-03 0
2008-01-04 1
...全文
271 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanglv676 2011-01-12
  • 打赏
  • 举报
回复
using(d_field)
using()是什么函数?
懒得去死 2008-02-22
  • 打赏
  • 举报
回复
加精化了,以后这个帖子做参考用
懒得去死 2008-02-22
  • 打赏
  • 举报
回复

mysql> use t_girl
Database changed
mysql> create table c_t (d_field date not null, n_field char(1) not null);
Query OK, 0 rows affected (0.00 sec)

mysql> create temporary table tmp (d_field date not null);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into tmp values('2008-01-01'),('200-01-02'),('2008-01-03'),('2008-01-04');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> load data infile '/tmp/t.sql' into table c_t;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select * from c_t;
+------------+---------+
| d_field | n_field |
+------------+---------+
| 2008-01-01 | A |
| 2008-01-01 | B |
| 2008-01-02 | C |
| 2008-01-04 | D |
+------------+---------+
4 rows in set (0.00 sec)

mysql> select a.d_field,sum((case when b.d_field is null then 0 else 1 end ))as num from tmp as a left join c_t as b using(d_field) group by a.d_field order by a.d_field asc;
+------------+------+
| d_field | num |
+------------+------+
| 2008-01-01 | 2 |
| 2008-01-02 | 1 |
| 2008-01-03 | 0 |
| 2008-01-04 | 1 |
+------------+------+
4 rows in set (0.00 sec)


rainbowliuliang 2008-02-21
  • 打赏
  • 举报
回复
支持
liuyann 2008-02-21
  • 打赏
  • 举报
回复

then

select c,cdate,count(a.Fd01)
from calendar c left jion tableA a on c.cdate=a.cdate
where c.cdate between '2008-01-01- and '2008-01-04'
group by c.cdate;

== 思想重于技巧 ==
liuyann 2008-02-21
  • 打赏
  • 举报
回复

create table calendar
(
cdate DATE constraint pk_calendar PRIMARY KEY
);

insert into calendar values ('2008-01-01');
insert into calendar values ('2008-01-02');
insert into calendar values ('2008-01-03');
...
insert into calendar values ('2009-01-01');


== 思想重于技巧 ==
xman1231 2008-02-21
  • 打赏
  • 举报
回复
。。。。。。
时间段不固定,日历表。。。怎么做啊。。
liuyann 2008-02-21
  • 打赏
  • 举报
回复

这个问题讨论了很多次.

解决方案

建一个日历表
2008-01-01
2008-01-02
2008-01-03
2008-01-04
...

然后用left join
== 思想重于技巧 ==
xman1231 2008-02-21
  • 打赏
  • 举报
回复
求SQL查询语句

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧