Why do I get this error? The SQL works fine when I run it in phpMyAdmin.
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 '' at line 1 (SQL: select id from users where active = true and birthyear != 0000 and not exists (select id from agestats where users.id = agestats.user_id and agestats.year = 2020-01-13)
SQL:
select id
from users
where active = true
and birthyear != 0000
and not exists (
select 1
from agestats
where users.id = agestats.user_id
and agestats.year = 2020
)
And the Laravel code:
$membersToInsert = DB::select(DB::raw(
'select id
from users
where active = true
and birthyear != 0000
and not exists (
select id
from agestats
where users.id = agestats.user_id
and agestats.year = ' . date('Y-m-d')
));
date('Y-m-d')
will be2020-01-13
, not2020
. 3. Date strings need to be quoted. 4. Line breaks like I've introduced will make error messages MUCH more relevant. – Sammitch Jan 13 '20 at 21:18