我试图通过仅显示上次检查后更新的结果(由 $last_check 定义)来获取联合结果
$last_check = CheckNotifications::where('notif_check_user_id', '=', $user_id)->pluck('updated_at');
$get_projects = DB::table('requests')
->join('notifications', 'notifications.notif_project_id', '=', 'requests.id')
->select(DB::raw('requests.updated_at, requests.request_name, "" as fullname, "P" as flag'))
->where('notif_user_id', '=', $user_id)
->whereRaw('requests.updated_at > requests.created_at');
$comments = DB::table('project_comments')
->leftJoin('users', 'comment_user_id', '=', 'users.id')
->leftJoin('requests', 'comment_project_id', '=', 'requests.id')
->select(DB::raw('project_comments.updated_at, requests.request_name, users.fullname, "C" as flag'));
$get_notifications = $get_projects->union($comments)
->whereRaw('updated_at > $last_check')
->orderBy('updated_at', 'desc')
->get();
在我添加最后一个 whereRaw 语句之前,联合工作得很好。添加该语句后,我收到 SQL 语法/访问错误 (#42000)。我认为这与为进行比较而提取 Updated_at 有关。有任何想法吗?提前致谢!
编辑:这是错误代码
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15:09:06) union (select project_comments.updated_at, requests.id as request_id, ' at line 1 (SQL: (select requests.updated_at, requests.id as request_id, requests.request_name, "" as fullname, "P" as flag from `requests` inner join `notifications` on `notifications`.`notif_project_id` = `requests`.`id` where `notif_user_id` = 1 and requests.updated_at > requests.created_at and requests.updated_at > 2015-07-29 15:09:06) union (select project_comments.updated_at, requests.id as request_id, requests.request_name, users.fullname, "C" as flag from `project_comments` left join `users` on `comment_user_id` = `users`.`id` left join `requests` on `comment_project_id` = `requests`.`id`) order by `updated_at` desc)
最佳答案
应该有2个updated_at列,请附上您的错误,同时再次提及类似的内容,我无法确定原因。
解决办法很简单,将表名写在列名之前 改变
->whereRaw('updated_at > $last_check')
进入
->whereRaw('requests.updated_at > $last_check')
并将 orederby() 更改为 whereRaw() 之后 你之前就写了几行正确的代码:D 我希望这能解决您的问题
关于php - 比较 Laravel 5 中两个不同表的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31688601/