Vague question are you using regular PHP or carbon date, if carbon have you looked at the documentation for carbon they have a wealth of examples.
May 31, 2016
3
Level 1
Another Between Date question
I'm trying to do a query like: Select employee_id from employees, company where employees.birthdate between company.beginDate and company.endDate..
I see examples where the query dates are constant values, but none where they were fields...
Thanks!
Level 54
The problem with that query isn't the where between clause, it's that you're trying to select from two tables without specifying how to join them.
I assume employees have a company_id on them, so the resulting SQL should look like this:
SELECT employee_id FROM employees
JOIN company ON employees.company_id = company.id
WHERE employees.birthdate BETWEEN company.beginDate and company.endDate
With query builder you could do it like this:
$employee_ids = DB::table('employees')
->leftJoin('company', 'employees.company_id', '=', 'company.id')
->whereBetween('employees.birthdate', ['company.startDate', 'company.endDate'])
->pluck('employee_id');
Please or to participate in this conversation.