2013-08-19 1 views
0

다음과 같은 쿼리가 있습니다.MySQL 한계가 특정 범위로 설정되지 않음

$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(); 

나는 다시 하나 개의 레코드를 끌어 할 따라서 "제한 (1)"하지만 여러 레코드를 다시 가져옵니다. 이 코드는 나에게 완벽 해 보인다. 어떤 도움이라도 대단히 감사하겠습니다.

건배.

상관 관계가있는 하위 쿼리가 작동합니까?

답변

0

정확한 이유를 얻기 위해 서로 다른 테이블 간의 관계를 볼 필요가 있지만, 내 생각에 테이블간에 일대 다 관계가 있고이를 요약하려면 GROUP BY이 필요합니다.

주 행을 한 결과 (예 : devices.id = x)로 제한하는 WHERE 절을 사용하여 쿼리를 실행하는 것이 좋습니다. 그런 다음 해당 ID에 대해 여러 행이 반환되며 GROUP BY와 같은 집계 함수를 사용하여 그 결과를 단일 행에 결합합니다.