MySQL Limit not setting to a specific range
I have a query as follows.
$tests = DB::select('devices.id')
->select(array('devices.description', 'dev_desc'))
->select(array('device_types.description', 'devtype_init'))
->select(array('actions.description', 'test_desc'))
->select(array('history.dt_logged', 'last_test'))
->select(array(DB::expr('history.dt_logged + (devices.frequency * 7 * 24
* 60 * 60)'), 'next_test'))
->from('history')
->join('actions')->on('history.action_id', '=', 'actions.id')
->join('devices')->on('history.device_id', '=', 'devices.id')
->join('device_types')->on('devices.device_type_id', '=',
'cal_device_types.id')
->and_where(DB::expr('history.dt_logged + (devices.frequency * 7 * 24 *
60 * 60)'), '>=', $start)
->and_where(DB::expr('history.dt_logged + (devices.frequency * 7 * 24 *
60 * 60)'), '<=', $end)
->where('device_types.description', '=', 'Calibration')
->order_by('history.dt_logged', 'desc')->limit(1)
->execute();
I want to pull back one record hence the " limit(1) " , but it pulls back
multiple records. The code looks perfect to me. Any help would be greatly
appreciated.
Cheers.
Would a correlated subquery work ?!
No comments:
Post a Comment