laravel 原始连接查询
laravel raw join query
我有一个接受原始 where 条件和连接的函数:
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT JOIN users ON data.user_id = users.id');
function query($table, $keys = [], $where = '', $joins = '') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)) {
$query=$query->?????????????
}
return $query->get();
}
如何将原始连接与查询构建器一起使用,就像我可以将 whereRaw 用于 where 条件一样?
我在这里提出了一些想法,如何让你的方法有点通用。对 where 子句和查询的其他部分执行相同的操作。
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT', 'users', 'data.user_id','users.id');
function query($table, $keys = [], $where = '', $join_type = 'LEFT', $join_table='', $join_field1='', $join_field2='') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)&&!empty($join_type)&&$join_type=='LEFT') {
$query=$query->leftJoin($join_table, $join_field1, '=', $join_field2)
}
if(!empty($joins)&&!empty($join_type)&&$join_type=='INNER') {
$query=$query->join($join_table, $join_field1, '=', $join_field2)
}
///and so on
return $query->get();
}
//But you can make this method more generic, I just put some idea so that you can make this method more flexible
//I did not take a look at your other part of the method though.
注意: 我没有将多个连接放在两个 table 连接中,但您可以使您的方法形式更通用,更灵活。我只是想把我脑海中浮现的一些想法说出来。如果我说错了请指正。
参考:
Laravel - Joins
如果您只是对所有内容使用原始表达式,请不要为查询生成器操心。
选项 1:利用 PDO
PDO 是 Laravel 使用的底层驱动程序。要获取 PDO 对象,运行:
$pdo = DB::connection()->getPdo();
选项 2:运行 原始查询
您可以 运行 通过 Laravel 整个选择而无需 "building" 查询:
DB::select("SELECT * FROM table WHERE ..");
这甚至允许在需要时绑定参数。
DB::table('data')->join('users','data.user_id','users.id')->where(~~~)
没有好闻但有效
\DB::table('data')->join('user', function($join){
$join->on(\DB::raw('( `data`.`user_id` = `user.id` or (`data`.`user_id` is null and `data`.`other_field` is null)) and 1 '),'=',\DB::raw('1'));
})
我有一个接受原始 where 条件和连接的函数:
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT JOIN users ON data.user_id = users.id');
function query($table, $keys = [], $where = '', $joins = '') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)) {
$query=$query->?????????????
}
return $query->get();
}
如何将原始连接与查询构建器一起使用,就像我可以将 whereRaw 用于 where 条件一样?
我在这里提出了一些想法,如何让你的方法有点通用。对 where 子句和查询的其他部分执行相同的操作。
query('data',['fieldA','fieldB'], 'fieldA > 10 AND fieldB < 20', 'LEFT', 'users', 'data.user_id','users.id');
function query($table, $keys = [], $where = '', $join_type = 'LEFT', $join_table='', $join_field1='', $join_field2='') {
$query = DB::table($table)->select($keys);
if(!empty($where)) {
$query=$query->whereRaw($where);
}
if(!empty($joins)&&!empty($join_type)&&$join_type=='LEFT') {
$query=$query->leftJoin($join_table, $join_field1, '=', $join_field2)
}
if(!empty($joins)&&!empty($join_type)&&$join_type=='INNER') {
$query=$query->join($join_table, $join_field1, '=', $join_field2)
}
///and so on
return $query->get();
}
//But you can make this method more generic, I just put some idea so that you can make this method more flexible
//I did not take a look at your other part of the method though.
注意: 我没有将多个连接放在两个 table 连接中,但您可以使您的方法形式更通用,更灵活。我只是想把我脑海中浮现的一些想法说出来。如果我说错了请指正。
参考:
Laravel - Joins
如果您只是对所有内容使用原始表达式,请不要为查询生成器操心。
选项 1:利用 PDO
PDO 是 Laravel 使用的底层驱动程序。要获取 PDO 对象,运行:
$pdo = DB::connection()->getPdo();
选项 2:运行 原始查询
您可以 运行 通过 Laravel 整个选择而无需 "building" 查询:
DB::select("SELECT * FROM table WHERE ..");
这甚至允许在需要时绑定参数。
DB::table('data')->join('users','data.user_id','users.id')->where(~~~)
没有好闻但有效
\DB::table('data')->join('user', function($join){
$join->on(\DB::raw('( `data`.`user_id` = `user.id` or (`data`.`user_id` is null and `data`.`other_field` is null)) and 1 '),'=',\DB::raw('1'));
})