| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 488 人关注过本帖
标题:[求助]这个SQL怎么写呢??
只看楼主 加入收藏
appltes
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-2
收藏
 问题点数:0 回复次数:1 
[求助]这个SQL怎么写呢??

我有一张表,结构和数据如下:
id start_time end_time
23 2006-7-10 8:50:10 2006-8-10 18:00:00
67 2006-7-2 8:10:02 2006-7-2 17:35:00
67 2006-7-3 9:00:23 2006-7-3 17:34:00
67 2006-7-4 9:30:34 2006-7-4 17:40:00
67 2006-7-5 8:45:22 2006-7-5 17:10:00
67 2006-8-1 8:40:12 2006-8-1 17:45:00
67 2006-8-2 10:20:12 2006-8-2 21:02:15

start_time与end_time均为时间类型

现在要写一个SQL要求如下:
1、取出start_time是2006年7月,end_time也是2006年7月,如果end_time超过7月的,就按到7月31号算。取start_time和end_time的天数差。

我自己写的:
select user_id,sum(datediff('d',starrt_time,end_time)) as days from tables where user_id=23 group by user_id
结果是:user_id days
23 30

我想达到的效果是:
select user_id,sum(datediff('d',starrt_time,end_time)) as days from tables where user_id=23 and ??? group by user_id
结果是:user_id days
23 20

不知道我有没有表达清楚我的意思,如果我没有说清楚,请大家提出来!
谢谢各位老大了!帮帮忙吧!

[此贴子已经被作者于2006-8-9 10:35:51编辑过]

搜索更多相关主题的帖子: SQL 
2006-08-09 10:34
a97191
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-1-18
收藏
得分:0 
select user_id,sum(datediff('d',starrt_time,(case when end_time>'2006-7-31' then '2006-7-31' else end_time end))) as days from tables where user_id=23 group by user_id
2006-08-09 11:38
快速回复:[求助]这个SQL怎么写呢??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved