我在以“laravel 方式”添加具有多个联合的查询时遇到问题。
我正在尝试完成与以下生成的查询等效的查询:
$ipsql = "";
for ($n = 1; $n < $total_networks; $n++) {
$ipsql .= "(SELECT * FROM ip WHERE network = " . $n . " AND used = 0 LIMIT 5)
UNION ALL";
}
if ($n == $total_networks) {
$ipsql .= "(SELECT * FROM ip WHERE network = " . $n . " AND used = 0 LIMIT 3) ORDER BY ip_addr";
}
我还没有找到与 Eloquent 合并的选项,所以我尝试为这个特定部分使用查询构建器,但在使用构建器 unionAll 时我一直遇到问题。
使用这个:
$ip_list = DB::table('ips')->where('network', '=', '0')->where('used', '=', '0')->limit(5);
for($n = 1; $n < $network_count; $n++){
$ip_list = DB::table('ips')->where('network', '=', $n)->where('used', '=', '0')->limit(5)->unionAll($ip_list);
}
$ips = $ip_list->get();
我不断收到 MySQL 语法错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'union all ((select * from `ips` where `network` = ? and `used` = ? limit 5) unio' at line 1
(SQL:
(select * from `ips` where `network` = 16 and `used` = 0 limit 5) union all ((select * from `ips`
where `network` = 15 and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 14
and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 13 and `used` = 0 limit 5)
union all ((select * from `ips` where `network` = 12 and `used` = 0 limit 5) union all ((select *
from `ips` where `network` = 11 and `used` = 0 limit 5) union all ((select * from `ips` where
`network` = 10 and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 9 and
`used` = 0 limit 5) union all ((select * from `ips` where `network` = 8 and `used` = 0 limit 5)
union all ((select * from `ips` where `network` = 7 and `used` = 0 limit 5) union all ((select * from
`ips` where `network` = 6 and `used` = 0 limit 5) union all ((select * from `ips` where `network` =
5 and `used` = 0 limit 5) union all ((select * from `ips` where `network` = 4 and `used` = 0 limit
5) union all ((select * from `ips` where `network` = 3 and `used` = 0 limit 5) union all ((select *
from `ips` where `network` = 2 and `used` = 0 limit 5) union all ((select * from `ips` where
`network` = 1 and `used` = 0 limit 5) union all (select * from `ips` where `network` = 0 and `used`
= 0 limit 5)))))))))))))))))
我可以从错误中看出它嵌套了每个新的联合调用,这造成了语法问题。 我尝试使用 DB::raw 完成相同的任务,但似乎也在某处搞砸了。有没有一种方法可以更适合 laravel? 感谢您的关注!
最佳答案
您的 unionAll
调用确实嵌套了。一种解决方案是在 for
循环中创建一个子查询,然后 unionAll
该子查询在定义后 到主查询。然后在完成后对整个 shebang 运行 get
,如下所示:
$ips_list = DB::table('ips')->where('network', '=', '1')->where('used', '=', '0')->limit(5);
for($n = 1; $n < $total_networks; $n++){
$ip_list_subquery = DB::table('ips')
->where('network', '=', $n)
->where('used', '=', '0')
->limit(5);
$ips_list = $ips_list->unionAll($ip_list_subquery);
}
$ips = $ips_list->get();
因此,实际上,您正在链接 unionAll
调用:
$a->unionAll($b)->unionAll($c)->unionAll($d)...
而不是嵌套它们:
$a->unionAll($b->unionAll($c->unionAll($d...))))
关于php - Laravel 多重联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25924592/