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 ?!
We would need to see the relationship between the different tables to get the exact reason, but my guess is that you have a one-to-many relationship across those tables, and will need a GROUP BY
to summarise them.
I suggest running your query with a WHERE
clause that limits the main row to one result (eg devices.id = x). You will then see multiple rows returned for that ID, use an aggregating function like GROUP BY to combine those results into a single row.
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.