我在这样的原始SQL中搜索功能
//test condition variable
if ($param_type != "") {
if($param_type =='SAS'){
$str .= " AND pos.type IN('SAS1','SAS2') ";
}
else if($param_type =='SME'){
$str .= " AND pos.type IN('SME1','SME2') ";
}
else if($param_type =='PT'){
$str .= " AND pos.type IN('PT','PT0') ";
}else{
$str .= " AND pos.type ='$param_type'";
}
}
//SQL
$query =" SELECT * FROM tbl_user AS u INNER JOIN tbl_position AS pos
ON u.user_id = pos.user_id where u.levels IN ('7','8','9') .$str ;" ;
如何在laravel中编写这样的SQL?
答案 0 :(得分:0)
您可以使用原始查询。
$query = DB::select(SELECT * FROM tbl_user AS u INNER JOIN tbl_position AS pos
ON u.user_id = pos.user_id where u.levels IN ('7','8','9') .$str ;");
当然,您需要在控制器的顶部添加use DB;
无论如何Raw查询可能是危险的,并且使用查询构建器会更好 简单的例子:
$query = DB::table('tbl_user')
->join('tbl_position','tbl_user.user_id','=','tbl_position.user_id')
->where('something','=','something')
->get();
查询构建器:query builder
答案 1 :(得分:0)
回答我的问题是谁遇到像我这样的问题:
像这样的例子
$query = DB::table('user');
if ($param_type =='PT')
$query->where('pos.type', '=', 1);
}
$result = $query->get();