首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >laravel如何管理数据库查询中的多个联合

问laravel如何管理数据库查询中的多个联合
EN

Stack Overflow用户
提问于 2019-04-09 03:23:55
回答 2查看 26关注 0票数 1

我的Laravel中有3个联合查询,该查询在SQL中有效,但在Laravel中不起作用。并显示以下错误:

代码语言:javascript
复制
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 

代码如下所示。

代码语言:javascript
复制
$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();

我真的不知道是什么导致了这个错误,但可能是因为我在3次中调用了联合?对这个有什么想法吗?

Union在只有两个的情况下是有效的,但是当我添加第三个时,它得到了错误

EN

回答 2

Stack Overflow用户

发布于 2019-04-09 03:42:23

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

代码语言:javascript
复制
$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();
票数 0
EN

Stack Overflow用户

发布于 2019-04-09 04:13:33

请尝试使用eloquent模型的关系,而不是DB。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55580443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档