So I'm trying to build a query to my SQL database from my backend (that's written in php with Laravel). I have a table in my database with a column that's titled "created_at", carrying values of the 'timestamp'-type. What I want to do is, when I send a query to my database, to only pick the rows (or instances, I guess) of my table that are created 30 days or less from the current date. I've tried experimenting back and forth with the 'where' keyword, but I couldn't get it to work as I wanted it to.

所以我正在尝试从我的后端构建一个查询到我的SQL数据库(用Laravel编写的php)。我的数据库中有一个表,其标题为“created_at”,带有'timestamp'类型的值。我想要做的是,当我向我的数据库发送查询时,只选择从当前日期30天或更短时间创建的表格的行(或我猜)。我尝试过使用'where'关键字来回实验,但我无法让它按照我的意愿运行。

When I browse the table in my database, the dates are written in the format "YYYY-MM-DD hh:mm:ss" but I'm not sure how to compare them when they're in this format, and I don't know how to convert them whilst doing a query. I've looked at the whereBetween() method in Laravel but I didn't get that to work since I didn't know how I would compare them.

当我在我的数据库中浏览表格时,日期以“YYYY-MM-DD hh:mm:ss”的格式写出,但我不确定如何以这种格式比较它们,我不知道知道如何在进行查询时转换它们。我已经查看了Laravel中的whereBetween()方法,但由于我不知道如何比较它们,所以我没有这样做。

At the moment my query looks like this:

目前,我的查询如下所示:

$topUser = DB::table('statistics')
            ->join('users', 'statistics.user_id', '=', 'users.id', 'inner')
            ->select('fname', 'user_id', DB::raw('sum(price) as total_collection'))
            ->groupBy('user_id')
            ->orderBy('total_collection','desc')
            ->get();

What I want to do, in pseudocode, is something along the lines of;

我想用伪代码做的事情就是这样;

->where 'created_at' <= 30 days ago

Thanks in advance!

提前致谢!

2 个解决方案

#1


1

It's basically that simple.

基本上这很简单。

->whereDate('created_at', '<=', \Carbon\Carbon::now()->subDays(30));

更多相关文章

  1. 在PHP中如何取得两个日期时间相减的结果,得出大于一天,大于二天,大
  2. 当excel表格单元格的格式是日期格式非文本格式的时候,phpexcel 应
  3. PHP格式化日期,显示“刚刚,几分钟前,几小时前,几天前,几个月前”
  4. 不正确的日期时间值:“item.purchase date”
  5. 在mysql shell中显示没有表行的查询结果(非表格输出)
  6. 格式化mysql表引入Attendace表格格式
  7. MySQL -如何在最小/最大日期差异超过3年的情况下选择id
  8. MySQL表格查询基本语句2
  9. 你可以在android webview中自动链接日期吗?

随机推荐

  1. Android(安卓)Studio 快捷键
  2. Android(一) 安卓概述
  3. Android Mac开发Android推荐软件
  4. android:layout_paddingLeft和android:la
  5. Android(安卓)NDK开发篇(一) windows免cygw
  6. Android引路蜂地图开发包
  7. Android Animation 大全
  8. Android(安卓)led灯实现大致流程
  9. Android客户端post请求服务器端实例
  10. Android中的线程机制