在laravel中使用select和count进行查询

时间:2018-05-04 13:04:59

标签: sql laravel eloquent

我在SQL中执行了此查询,但我无法在Eloquent中使用它。 请帮帮我。

SELECT 
    tags.desc,
    COUNT(planificacion_info.id_area) as cantidad_intervenciones
FROM 
    tags
    INNER JOIN planificacion_info ON planificacion_info.id_area = tags.id_tag
WHERE 
    tags.grupo = 'area' and tags.estado = true
GROUP BY
    tags.desc

2 个答案:

答案 0 :(得分:2)

你可以这样做:

$result = \DB::table('tags')->selectRaw('tags.desc, COUNT(planificacion_info.id_area) as cantidad_intervenciones')
            ->join('planificacion_info', 'planificacion_info.id_area', '=', 'tags.id_tag')
            ->where('tags.grupo', 'area')
            ->whereRaw('tags.estado = true')
            ->groupBy('tags.desc')
            ->get();

给你这个要求:

SELECT 
  tags.desc, 
  COUNT(planificacion_info.id_area) AS cantidad_intervenciones 
FROM 
  `tags` 
  INNER JOIN `planificacion_info` ON `planificacion_info`.`id_area` = `tags`.`id_tag` 
WHERE 
  `tags`.`grupo` = 'area' 
  AND tags.estado = TRUE 
GROUP BY 
  `tags`.`desc`

答案 1 :(得分:0)

这将是谎言

$tags= DB::table('tags')
    ->select('tags.desc',DB::raw("COUNT(planificacion_info.id_area) AS cantidad_intervenciones")
    ->innerJoin('planificacion_info','planificacion_info.id_area','=','tags.id_tag')
    ->where('tags.grupo','area')
    ->where('tags.estado',true)
    ->groupBy('tags.desc')
    ->get();