php - 拉拉维尔 : Order by string date using query builder with MySql

标签 php mysql laravel

该表有一个 VARCHAR 类型的 startDate 列,因此需要使用该列按升序获取行。

尝试过这个:

orderBy(DB::raw("DATE(startDate)"), 'ASC')

上面没有返回正确的顺序,如何通过clouse按顺序传递字符串日期?

示例:

'startDate' => '2017 年 11 月 7 日'

$items = DB::table("mytable")->orderBy(DB::raw("DATE(startDate)"), 'ASC')->where('userId','=' ,$userId)->get();

最佳答案

您需要将日期转换为 MySQL 格式。尝试使用DATE_FORMAT:

$items = DB::table("mytable")
           ->orderBy(DB::raw("DATE_FORMAT(startDate,'%d-%M-%Y')"), 'ASC')
           ->where('userId','=',$userId)
           ->get();

关于php - 拉拉维尔 : Order by string date using query builder with MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940994/

相关文章:

java - 如何显示多个检索数据的项目 MySQL 和 Android

php - 这个简单的查询有问题。有人能看到吗?

javascript - 使用 react-responsive-tabs 时更新 React 组件

php - 更新到 Laravel 7 php artisan 后无法正常工作

php - 使用 Laravel 获取数组键而不是值

php - 愚蠢,无法弄清楚 php 向下舍入 :(

php - 如何使用 PHP 设置 HTML 选择框的选定值

php - Laravel 5.2.31 Socialite 证书 ssl 错误

php - WanWizard datamapper orm 中关系的组合结果?

php - 使用swift2插入数据库