jeudi 11 août 2016

Laravel query builder join with multiple subquery

I has try to convert query in mysql to laravel query builder but it's not working. My query in sql something like this :

SELECT *
FROM claim.t_meter a
JOIN
  (
  SELECT MAX(REPLACEMENT_COUNT) AS MAX_REPLACEMENT_COUNT, meter_no
  FROM claim.t_meter
  WHERE bukken_uid = 1. AND deleted = 0
  GROUP BY meter_no
) b ON a.meter_no = b.meter_no AND a.REPLACEMENT_COUNT = b.MAX_REPLACEMENT_COUNT
LEFT JOIN
  (
  SELECT NAME, meter_no, INSPECTION_GROUP_UID
  FROM claim.m_inspection_group c
  JOIN claim.t_inspection_group_setting d ON c.uid = d.inspection_group_uid
) e ON a.meter_no = e.meter_no
GROUP BY a.meter_no
ORDER BY inspection_group_uid ASC

Now i'm using :

$query  = ' SELECT * FROM t_meter a JOIN (';
$query .= ' SELECT MAX(REPLACEMENT_COUNT) AS MAX_REPLACEMENT_COUNT, meter_no';
$query .= ' FROM t_meter WHERE bukken_uid = '.$id.' AND deleted = 0 GROUP BY meter_no)';
$query .= ' b ON a.meter_no = b.meter_no AND a.REPLACEMENT_COUNT = b.MAX_REPLACEMENT_COUNT';
$query .= ' LEFT JOIN (SELECT NAME,meter_no,INSPECTION_GROUP_UID FROM m_inspection_group c';
$query .= ' JOIN t_inspection_group_setting d ON c.uid = d.inspection_group_uid';
$query .= ' ) e ON a.meter_no = e.meter_no GROUP BY a.meter_no ORDER BY inspection_group_uid ASC';
$arrInFo = DB::select($query);

It's not best way, please help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire