加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 在laravel查询构建器中使用多个where子句

发布时间:2020-12-14 19:45:25 所属栏目:大数据 来源:网络整理
导读:我在转换以下SQL查询以使用laravels查询构建器时遇到了很多麻烦. SELECT * FROM gifts JOIN giftcategory ON gifts.id = giftcategory.giftidJOIN giftoccasions ON gifts.id = giftoccasions.giftidJOIN giftrelationship ON gifts.id = giftrelationship.g
我在转换以下SQL查询以使用laravels查询构建器时遇到了很多麻烦.
SELECT * FROM gifts 
JOIN giftcategory ON gifts.id = giftcategory.giftid
JOIN giftoccasions ON gifts.id = giftoccasions.giftid
JOIN giftrelationship ON gifts.id = giftrelationship.giftid

WHERE (gifts.gender = 'any' OR gifts.gender = 'male')
AND giftoccasions.occasionid = '2'
AND (giftcategory.categoryid = '0' OR giftcategory.categoryid = '1')
AND giftrelationship.relationshipid = '1'

此查询工作正常,但在使用Laravels查询构建器时,我无法获得相同的结果.到目前为止,我有以下代码.它根本不能正常工作.我认为问题可能在于orWhere部分,因为它似乎返回的结果与其他where子句不匹配.

$giftQuery = DB::Table('gifts')
->Join('giftcategory','gifts.id','=','giftcategory.giftid')
->Join('giftoccasions','giftoccasions.giftid')
->where('gifts.gender',"male")
->orwhere('gifts.gender',"any")
->where('giftoccasions.occasionid',"2")
->where('giftoccasions.relationshipid',"1")
->Where('giftcategory.categoryid',"0")
->orWhere('giftcategory.categoryid',"1");
您想将 advanced where与参数分组一起使用:
$giftQuery = DB::table('gifts')
    ->join('giftcategory','giftcategory.giftid')
    ->join('giftoccasions','giftoccasions.giftid')
    ->where(function($query) {
        $query->where('gifts.gender',"male")
            ->orWhere('gifts.gender',"any");
    })
    ->where('giftoccasions.occasionid',"2")
    ->where('giftoccasions.relationshipid',"1")
    ->where('giftcategory.categoryid',"0")
    ->orWhere('giftcategory.categoryid',"1");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读