Doubt with sql on Laravel?

Asked

Viewed 41 times

1

Today I have a query that is returning me duplicate data, I made the change of sql more needed to change it.

As I can leave in the same structure of select(DB::raw?

The current code looks like this:

    $atividades = $this->atividade->select(DB::raw
        ('sum(num_total_pessoas) as total_pessoas,
          sum(convite_prod) as total_convite_prod,
          sum(convite_apaa) as total_convite_apaa,
          sum(educativo_producao) as total_educativo_producao,
          sum(educativo_apaa) as total_educativo_apaa,
          sum(atend_social_producao) as total_atend_social_producao,
          sum(atend_social_apaa) as total_atend_social_apaa,
          MONTH(data) as data2,
          data_fim,plano_id,
          programa_id,
          tipo_publico_id'))->
    where('plano_id',$id)->
    groupBy('tipo_publico_id','data2','programa_id')->get();

my new consultation this so:

       echo "<hr/>teste de consulta nova <br/>";
   $results = DB::select(' select 
                            sum(a.num_total_pessoas) as total_pessoas,
                            sum(a.convite_prod) as total_convite_prod,
                            sum(a.convite_apaa) as total_convite_apaa,
                            sum(a.educativo_producao) as total_educativo_producao,
                            sum(a.educativo_apaa) as total_educativo_apaa,
                            sum(a.atend_social_producao) as total_atend_social_producao,
                            sum(a.atend_social_apaa) as total_atend_social_apaa,
                            MONTH(a.data) as data2,
                            a.data_fim,plano_id,
                            a.programa_id,
                            a.tipo_publico_id
                            from atividades a 
                            group by a.tipo_publico_id');
   var_dump($results);

1 answer

1


Just pack the group by from the previous query, no need to remove DB::raw.

$atividades = $this->atividade->select(DB::raw
    ('sum(num_total_pessoas) as total_pessoas,
      sum(convite_prod) as total_convite_prod,
      sum(convite_apaa) as total_convite_apaa,
      sum(educativo_producao) as total_educativo_producao,
      sum(educativo_apaa) as total_educativo_apaa,
      sum(atend_social_producao) as total_atend_social_producao,
      sum(atend_social_apaa) as total_atend_social_apaa,
      MONTH(data) as data2,
      data_fim,plano_id,
      programa_id,
      tipo_publico_id'))->
where('plano_id',$id)->
groupBy('tipo_publico_id')->get(); // alteração foi feita aqui
  • thanks for the help, I work with Asp.net mvc, was suffering with this php all day, learning in race

Browser other questions tagged

You are not signed in. Login or sign up in order to post.