我有这样的查询:

    $query = "Select EXISTS(SELECT * FROM grp_user AS pivot JOIN groups
AS g ON pivot.grp_id = g.id JOIN vote ON g.id = vote.grp_id WHERE
pivot.grp_id = :grp_id AND pivot.user_id = :user_id)";


我想运行它。所以我尝试了:

$res = DB::select($query,array("grp_id"=>250,"user_id"=>1));


理想情况下,res应该具有布尔值,但是它具有类似以下的数组:

[{"EXISTS(SELECT * FROM grp_user AS pivot JOIN groups AS g ON pivot.grp_id = g.id JOIN vote ON g.id = vote.grp_id WHERE pivot.grp_id = ? AND pivot.user_id = ?)":1}]


请注意最后的1,即我感兴趣的值(它为1或0)。

我该如何进行这项工作,或以“ laravel”方式实现这一目标?

最佳答案

为什么不检查它是否为空:

$query = "Select * FROM grp_user AS pivot JOIN groups
AS g ON pivot.grp_id = g.id JOIN vote ON g.id = vote.grp_id WHERE
pivot.grp_id = :grp_id AND pivot.user_id = :user_id LIMIT 1";

$res = DB::select($query,array("grp_id"=>250,"user_id"=>1));

return (count($res) > 0) ? 1 : 0;

关于php - Laravel运行Select Exists查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38104608/

10-13 09:12