sql语句的编写?

xxdsjq2005 2008-12-04 10:46:50
能够得到这样格式的时间 2008-12-03 2007-11-13。
编写sql语句实现这样几个统计信息:
今日统计:sql?
本月统计:sql?
今年统计:sql?
...全文
180 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
phpboy 2008-12-06
  • 打赏
  • 举报
回复
先计算出昨天的日期:




$time=time()-24*60*60; //昨天的时期戵

$date=date('Y-m-d',$time); //昨天的格式后日期


昨日统计:"slect * from a where tempDate='".$date."'"


penglewen 2008-12-06
  • 打赏
  • 举报
回复
昨天的:
$sql = 'slect * from a where tempDate=DATE_SUB('.date("Y-m-d").',INTERVAL 1 DAY)'
xxdsjq2005 2008-12-06
  • 打赏
  • 举报
回复
date("Y-m-d")得到的时间是今天,昨天该如何表达?
xxdsjq2005 2008-12-05
  • 打赏
  • 举报
回复
能够得到这样格式的时间 2008-12-03 2007-11-13。
编写sql语句实现这样几个统计信息:
今日统计:slect * from a where 时间是今天(怎么表达)
本月统计:slect * from a where 时间是本月(怎么表达)
今年统计:slect * from a where 时间是今年(怎么表达)
phpboy 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 motol 的回复:]
引用 6 楼 motol 的回复:
今日统计:"slect * from a where tempDate='".date("Y-m-d")."'"
本月统计:"slect * from a where MONTH(tempDate)='".date("m")."'"
今年统计:"slect * from a where YEAR(tempDate)='".date("Y")."'"


不好意思,上面的本月统计漏了个条件:"slect * from a where MONTH(tempDate)='".date("m")."' and YEAR(tempDate)='".date("Y")."'"
[/Quote]

UP
motol 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 motol 的回复:]
今日统计:"slect * from a where tempDate='".date("Y-m-d")."'"
本月统计:"slect * from a where MONTH(tempDate)='".date("m")."'"
今年统计:"slect * from a where YEAR(tempDate)='".date("Y")."'"
[/Quote]

不好意思,上面的本月统计漏了个条件:"slect * from a where MONTH(tempDate)='".date("m")."' and YEAR(tempDate)='".date("Y")."'"
penglewen 2008-12-05
  • 打赏
  • 举报
回复

今日统计:"slect count(*) as countnum from a where tempDate='".date("Y-m-d")."'"
本月统计:"slect count(*) as countnum from a where MONTH(tempDate)='".date("m")."'"
今年统计:"slect count(*) as countnum from a where YEAR(tempDate)='".date("Y")."'"
tempDate为你表中的时间字段。。。。
jiang_jiajia10 2008-12-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gingzai777 的回复:]
引用楼主 xxdsjq2005 的帖子:
能够得到这样格式的时间 2008-12-03 2007-11-13。

这个容易,SQL语句为:

SQL codeSELECT DATE_FORMAT( now( ) , '%Y-%m-%d' ) AS time;


测试过,可以的
[/Quote]
UP
phpboy 2008-12-05
  • 打赏
  • 举报
回复
楼主的意思也就是要实现按时间查询表中内容:

1.在建表的时候要把数据存入数据库的日期记入库中,假如记为year,mon,date;

2.存入数据库前计算今天的时间:$year=date('Y');$mon=date('n');$date=date('j');具体时间不用管了

3.在统计数据的时候就可以实现楼主想要的效果了:

在查询之前,计算现在的 $year,$mon,$date,计算方法同上!

今日统计:slect * from a where date=$date;
本月统计:slect * from a where mon=$mon;
今年统计:slect * from a where year=$year;
motol 2008-12-05
  • 打赏
  • 举报
回复
今日统计:"slect * from a where tempDate='".date("Y-m-d")."'"
本月统计:"slect * from a where MONTH(tempDate)='".date("m")."'"
今年统计:"slect * from a where YEAR(tempDate)='".date("Y")."'"
cyxin2121921 2008-12-05
  • 打赏
  • 举报
回复
mark
iasky 2008-12-05
  • 打赏
  • 举报
回复
没有看懂楼主的意思
xxdsjq2005 2008-12-05
  • 打赏
  • 举报
回复
再增加一条,昨日统计,怎么表达?
kimipolo 2008-12-05
  • 打赏
  • 举报
回复
select  date_format(now(),get_format(date,'iso')) as date;
Zijian_Zhang 2008-12-04
  • 打赏
  • 举报
回复
至于统计,不知楼主讲什么统计了??
Zijian_Zhang 2008-12-04
  • 打赏
  • 举报
回复
字段为time,DATE_FORMAT( now( ) , '%Y-%m-%d' )

这个语句的

now()是当前时间,这个还可以是其它的日期

DATE_FORMAT是MYSQL的函数,日期格式化,%Y-%m-%d是格式形式
Zijian_Zhang 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 xxdsjq2005 的帖子:]
能够得到这样格式的时间  2008-12-03      2007-11-13。
[/Quote]
这个容易,SQL语句为:
SELECT DATE_FORMAT( now( ) , '%Y-%m-%d' ) AS time;

测试过,可以的

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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