Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jason100's avatar

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!

0 likes
3 replies
jlrdw's avatar

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.

willvincent's avatar
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.