dongnuo3749 2015-07-28 23:03
浏览 176

比较Laravel 5中两个不同表的时间戳

I am trying to get results out of union by showing only results that were updated after I last checked (defined by $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();

The union works just fine until I add that last whereRaw statement. With the statement added, I get a SQL Syntax/Access error (#42000). I think it has something todo with the plucking of updated_at for the comparison. Any ideas? Thanks in advance!

Edit: Here's the error code

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条回答 默认 最新

  • drl971115 2015-07-29 00:02
    关注

    there whould be 2 updated_at column , Please attach your error while mentioning something like that again I can't be sure of the reason.

    the solution is simply to write the table name before the column name change

       ->whereRaw('updated_at > $last_check')
    

    into

      ->whereRaw('requests.updated_at > $last_check')
    

    and change orederby() to be after the whereRaw() you had written the right code just few lines before :D I hope this solve your problem

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题