使用MySQL时我是新手我遇到了以下问题:
我正在尝试使用以下条件执行查询:
给定一个包含created_at timestamp列的posts表 一个返回日期的查询(没有时间组件),一些帖子 对于给定日期和正在运行(累计)的帖子总数 直到某个特定日期。应该订购结果集 按日期顺序排列。
期望的输出:
date | count | total
-----------+-------+-------
2017-01-26 | 20 | 20
2017-01-27 | 17 | 37
2017-01-28 | 7 | 44
2017-01-29 | 8 | 52
...
我试过但是我收到了一个错误:
没有返回任何行
这就是我所拥有的:
set @cumsum:=0
select date(created_at) as 'date',count(date),(@cumsum=@cumsum+date) as total from posts
order by 'date'
group by 'date','count','total'
示例数据:
id created_at title
1 2017-12-25 04:35:08 +0000 Quos ut cupiditate quis.
2 2017-12-25 18:56:42 +0000 Exercitationem sunt ab modi facilis qui mollitia excepturi.
3 2017-12-25 10:31:08 +0000 Ex consectetur odit quasi.
4 2017-12-25 16:31:20 +0000 Reiciendis similique officiis dolorum blanditiis voluptatem veritatis cum.
5 2017-12-25 15:06:42 +0000 Explicabo ab et enim voluptatem id sunt eius nihil.
6 2017-12-25 15:58:27 +0000 Reiciendis ut et consequatur rerum accusamus architecto ea.
7 2017-12-25 04:09:35 +0000 Doloremque odio dolore aperiam illo.
任何帮助将不胜感激
先谢谢!
答案 0 :(得分:1)
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'label' => 'Country',
'value' => function ($model) {
return Country::findOne($model->country_id)->country_name;
},
],
],
]);
正确获取日期时间部分。 这样的事情:
CAST AS DATE
<强> Results 强>:
SET @total:=0;
SELECT
t.`Date`,
t.Total,
(@total := @total + t.Total) AS CummlativeTotal
FROM
(
SELECT
CAST(created_at AS DATE) AS `Date`,
COUNT(*) AS Total
FROM posts
WHERE created_at <= '2017-01-28'
GROUP BY CAST(created_at AS DATE)
ORDER BY CAST(created_at AS DATE)
) AS t;