Query that returns mysql value

0

I would like to know if there is any query in MYSQL that allows to return a value, in case that the query is affirmative, as is the query.

select usuario_ad, nombre, asignacion 
from usuarios 
where usuario_ad='danielad' and asignancion='paolaad';
    
asked by zereft 11.11.2018 в 00:53
source

1 answer

1

Going back to the idea of @ A.Cedano it occurs to me that with the query Builder you do it in the following way

QUERY BUILDER CODE

$data = \DB::table('usuarios') 
           ->selectRaw('COUNT(*) total') 
           ->where('usuario_ad', 'danielad') 
           ->where('asignacion', 'paolaad') 
           ->get(); 

if($data > 0)
{ 
  return 1; 
}else{ 
  return 0; 
}

EXPLANATION

  • We use selectRaw() to make the COUNT() of the column
  • You have the use of a WHERE with the operator AND to make it necessary for 2 conditions to be met; here we use two methods WHERE
  • With a if we do a check, if the variable that contains the query is greater than 0 then if there is data, otherwise we return 0
  • CODE WITH ELOQUENT

    $data = ModelName::where('usuario_ad', 'danielad')
            ->where('asignacion', 'paolaad')
            ->count()
            ->get();
    
    if($data > 0){
      return 1;
    }else{
      return 0;
    }
    

    EXPLANATION

    You can if you are using a model for the table usuarios assign it where I placed ModelName that is replace it

        
    answered by 11.11.2018 / 03:40
    source