1 、在 publish 表有一个 pubtime 字段用来存储发帖的时候的时间戳( 10 位),现在我想写一句 sql 查询语句 求当天所有的帖子总量!
下面的查询语句是我想了一晚上的结果,不过没有什么卵用!
求高手指点下。
select count(*) from publish Where DATE_FORMAT(FROM_UNIXTIME(pubtime),'Y-m-d')) = DATE_FORMAT(NOW(),'Y-m-d');
1
lg2016 OP 大家还没上班?
|
2
zouguolvyi 2016-04-15 09:41:18 +08:00
可以在程序中将当天的时间戳范围算出来,然后用 between and 来解决。
|
3
dangyuluo 2016-04-15 10:09:08 +08:00 via iPhone
建议你多睡点觉,调整一下作息
|
4
onion83 2016-04-15 10:25:32 +08:00 via iPhone
离真相近了,给个思路,用 group by
|
5
bianzhifu 2016-04-15 10:59:30 +08:00 1
SELECT
from_unixtime(pubtime, '%Y-%m-%d'), COUNT(*) FROM publish GROUP BY from_unixtime(pubtime, '%Y-%m-%d'); |
8
bianzhifu 2016-04-15 11:12:35 +08:00
SELECT
count(*) FROM publish WHERE pubtime >= floor(unix_timestamp(now()) / 86400) * 86400 - 28800 |
11
bianzhifu 2016-04-15 11:20:09 +08:00
@lg2016 更新下 sql 上面的 sql 错误了 没有考虑到时间戳 从 8 点开始
SELECT count(*) FROM publish WHERE pubtime >= floor( (unix_timestamp(now()) + 28800) / 86400 ) * 86400 - 28800 |
12
peter999 2016-04-15 11:24:31 +08:00
不如发帖的时候累加到记录表里,节省开销
|
13
lg2016 OP @zouguolvyi @onion83 根据你们的思路,写出下面这段查询语句
$today = date('Y-m-d'); $tomorrow = date('Y-m-d',strtotime('+1 day')); $sql = "select DATE_FORMAT(FROM_UNIXTIME(pubtime),'%Y-%m-%d') as todayr_date,count(*) as today_sum from publish where DATE_FORMAT(FROM_UNIXTIME(pubtime),'%Y-%m-%d') between '{$today}' and '{$tomorrow}' group by todayr_date"; |