How do I store 2 SQL queries in a single variable?

Asked

Viewed 58 times

1

Good afternoon guys, I am trying to make 2 queries in my application and I want both to be stored in a single variable, can I do ? if yes as I do ?

Consultation 1:

$usuarios = DB::table('users')
    ->join('user_municipios', 'user_municipios.user_id', '=', 'users.id')
    ->join('municipios', 'municipios.id', '=', 'user_municipios.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 'users.minicurriculo','users.fotoPerfil', 'users.plano_id', 'perfis.perfil', 'municipios.municipio', 'municipios.uf')
    ->where('user_municipios.municipio_id', '=', $municipio_id)->orderBy('users.plano_id', 'DESC')
    ->inRandomOrder()->paginate(25);

Query 2:

$usuarios = DB::table('users')->where('users.municipio_id', '=', $municipio_id)
    ->join('municipios', 'municipios.id', '=', 'users.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 'users.minicurriculo','users.fotoPerfil', 'users.plano_id', 'perfis.perfil', 'municipios.municipio', 'municipios.uf')
    ->orderBy('users.plano_id', 'DESC')->inRandomOrder()->paginate(25);
  • Why don’t you put an Or on the Where?

  • It did not work, because the second query 1 of a Join in the table user_municipios and the query 2 no.

1 answer

1


Makes a UNION in the since by your code the returned fields are the same and the two queries would be with these changes:

$usuarios1 = DB::table('users')
    ->join('user_municipios', 'user_municipios.user_id', '=', 'users.id')
    ->join('municipios', 'municipios.id', '=', 'user_municipios.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 
             'users.minicurriculo','users.fotoPerfil', 
             'users.plano_id', 'perfis.perfil', 
             'municipios.municipio', 'municipios.uf')
    ->where('user_municipios.municipio_id', '=', $municipio_id);    


$usuarios = DB::table('users')->where('users.municipio_id', '=', $municipio_id)
    ->join('municipios', 'municipios.id', '=', 'users.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 
             'users.minicurriculo','users.fotoPerfil', 
             'users.plano_id', 'perfis.perfil', 
             'municipios.municipio', 'municipios.uf')
    ->orderBy('users.plano_id', 'DESC') 
    ->inRandomOrder()
    ->union($usuarios1); //juntando as duas consultas ...
    ->get(25);

but, that way the paginate that you need does not work, because of the peculiar generation of SQL that is made by this method, but, there is a way to get around this:

$usuarios1 = DB::table('users')
    ->join('user_municipios', 'user_municipios.user_id', '=', 'users.id')
    ->join('municipios', 'municipios.id', '=', 'user_municipios.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 
             'users.minicurriculo','users.fotoPerfil', 
             'users.plano_id', 'perfis.perfil', 
             'municipios.municipio', 'municipios.uf')
    ->where('user_municipios.municipio_id', '=', $municipio_id);   


$usuarios = DB::table('users')->where('users.municipio_id', '=', $municipio_id)
    ->join('municipios', 'municipios.id', '=', 'users.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 
             'users.minicurriculo','users.fotoPerfil', 
             'users.plano_id', 'perfis.perfil', 
             'municipios.municipio', 'municipios.uf')
    ->orderBy('users.plano_id', 'DESC') 
    ->inRandomOrder()
    ->union($usuarios1);

Now with the union in the variable $usuarios do the following:

$sql = $usuarios->toSql();
$res = \DB::table(\DB::raw("($sql order by users.plano_id) a"))
            ->mergeBindings($usuarios->getQuery());

return $res->paginate(25);

Code in full:

$usuarios1 = DB::table('users')
    ->join('user_municipios', 'user_municipios.user_id', '=', 'users.id')
    ->join('municipios', 'municipios.id', '=', 'user_municipios.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 
             'users.minicurriculo','users.fotoPerfil', 
             'users.plano_id', 'perfis.perfil', 
             'municipios.municipio', 'municipios.uf')
    ->where('user_municipios.municipio_id', '=', $municipio_id);   


$usuarios = DB::table('users')->where('users.municipio_id', '=', $municipio_id)
    ->join('municipios', 'municipios.id', '=', 'users.municipio_id')
    ->join('perfis', 'perfis.id', '=', 'users.perfil_id')
    ->select('users.id', 'users.municipio_id', 'users.name', 
             'users.minicurriculo','users.fotoPerfil', 
             'users.plano_id', 'perfis.perfil', 
             'municipios.municipio', 'municipios.uf')
    ->orderBy('users.plano_id', 'DESC') 
    ->inRandomOrder()
    ->union($usuarios1);

$sql = $usuarios->toSql();
$res = \DB::table(\DB::raw("($sql order by users.plano_id) a"))
            ->mergeBindings($usuarios->getQuery());

return $res->paginate(25);

Observing: that orderBy with inRandomOrder it will never work, or one or the other and I left as it was the right maybe be done in $res thus:

return $res->inRandomOrder()->paginate(25);

Reference

  • Not for sure, Virgilio

  • @Brunosantos is to work out, which is the mistake?

  • @Brunosantos your questions never have a nice interaction with who answers, you leave a lot of information outstanding, "did not work" must have happened something that does not work, so expose the error or else because it went wrong, we want to help you but, so it gets a little difficult I hope you understand... The above code is correct and functional only maybe something is missing that has not been said!

  • at the end of the code when I try to execute like this: ->Union($usuarios1)->get(); it works, but when I try ->paginate(25) instead of ->get() the following error: Queryexception in Connection.php line 647: Queryexception in Connection.php line 647: SQLSTATE[21000]: Cardinality Violation: 1222 The used SELECT statements have a Different number of Columns (SQL: (select Count(*) as Aggregate from users Inner Join municipios on municipios.id = users.municipio_id Inner Join perfis on perfis.id = users.perfil_id Where users.municipio_id = 306) Union (select users.

  • I can’t paste all the error here

  • It really works but I need to use ->paginate() the place of ->get()

  • Test the ->simplePaginate(25) and it worked, but I can’t with the ->paginate();

  • @Brunosantos I did the editing, it has to be suitable to your ai, but, it has to be done so, look at the observations and any problem let me know!

Show 3 more comments

Browser other questions tagged

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