laravel如何在数据库查询中管理多个并集

时间:2019-04-08 19:23:55

标签: mysql sql laravel

我的Laravel中有3个联合查询,该查询在SQL中有效,但在Laravel中则无法。并显示此错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union all 

代码如下。

$android = DB::connection("mysql2")->table('android')
     ->join('game_players', 'game_players.id', '=', 'android.playerId')
     ->select("android.playerId", "android.dateAdded", "android.identifier")
     ...
     ->where("android.dateAdded", "<>", '');

$ios = DB::connection("mysql2")->table('ios')
     ->join('game_players', 'game_players.id', '=', 'ios.playerId')
     ->select("ios.playerId", "ios.dateAdded", "ios.identifier")
     ...
     ->unionAll($android);

$steam = DB::connection("mysql2")->table(steam')
     ->join('game_players', 'game_players.id', '=', 'steam.playerId')
     ->select("playerId", "dateAdded", "identifier")
     ...
     ->unionAll($ios)
     ->get();

我真的不知道是什么原因导致了这个错误,但也许是因为我三次打电话给工会?关于这个有什么想法吗?

如果只有两个,联盟会起作用,但是当我添加第三个联盟时,出现错误

2 个答案:

答案 0 :(得分:0)

我的错误..这就是答案:

$android = DB::connection("mysql2")->table('android')
     ->join('game_players', 'game_players.id', '=', 'android.playerId')
     ->select("android.playerId", "android.dateAdded", "android.identifier")
     ...
     ->where("android.dateAdded", "<>", '');

$ios = DB::connection("mysql2")->table('ios')
     ->join('game_players', 'game_players.id', '=', 'ios.playerId')
     ->select("ios.playerId", "ios.dateAdded", "ios.identifier")
     ...


$steam = DB::connection("mysql2")->table(steam')
     ->join('game_players', 'game_players.id', '=', 'steam.playerId')
     ->select("playerId", "dateAdded", "identifier")
     ...
     ->union($ios)
    ->union($android);
     ->get();

答案 1 :(得分:0)

请尝试使用雄辩的模型关系而不是数据库。

相关问题