我有一个SQL查询,如下所示:
SELECT * FROM AS T
INNER JOIN TABLE2 AS T2 ON T1.id =T2.tid
WHERE (t1.userid= $userid AND t1.unitid IN (2,3))
OR (t2.requesterid=$userid AND t2.requestertype IN(1,5))
使用Laravel中的原始查询,我可以这样做:
DB::table("TABLE1 as T1")
->join("TABLE2 as T2", "T1.userttype", " =","T2.tid")
->whereRaw("(t1.userid= $userid AND t1.unitid IN (2,3)) OR (t2.requesterid=$userid AND t2.requestertype IN(1,5)")->get();
但是如何通过Laravel或者其他结构实现这一目标。我试过它:
DB::table("TABLE1 as T1")
->join("TABLE2 as T2", "T1.userttype", "=", "T2.tid")
->where(function($query) use ($userid) {
$query->where("t1.userid", $userid)
->whereIn("t1.unitid", [2,3])
->orwhere("t2.requesterid", $userid)
->whereIn("t2.requestertype", [1,5])
});
如何做到这一点?
答案 0 :(得分:2)
你应该尝试两个内部功能。试试这个:
DB::table("TABLE1 as T1")
->join("TABLE2 as T2", "T1.userttype", "=", "T2.tid")
->where(function($query) use ($userid) {
$query->where("t1.userid", $userid)
->whereIn("t1.unitid", [2,3]);
})->orWhere(function($query) use ($userid) {
$query->where("t2.requesterid", $userid)
->whereIn("t2.requestertype", [1,5]);
});