简体   繁体   中英

Comparing timestamps from two different tables in 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) 

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 :DI hope this solve your problem

add that whereRaw clause for each of previous two queries

 ->where('requests.updated_at','>', $last_check)

or

->whereRaw('requests.updated_at > ?', [$last_check])

probably where is not available with union

just curious : if this is working?

$get_notifications = $get_projects->union($comments)
    ->where(function($query) use ($last_checked){
        $query ->where('requests.updated_at','>', $last_check);
    })
    ->orderBy('requests.updated_at', 'desc')
    ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM