php Laravel Eloquent Union 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41756404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:27:53  来源:igfitidea点击:

Laravel Eloquent Union query

phpmysqllaraveleloquentlaravel-5.3

提问by rotaercz

So I have the following query:

所以我有以下查询:

$a = Model::where('code', '=', $code)
    ->where('col_a', '=' , 1)
    ->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))

$b = Model::where('code', '=', $code)
    ->where('col_b', '=' , 1)
    ->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))

$a->union($b)->get();

No sorting is happening when I 'orderBy()' first and then union.

当我先 'orderBy()' 然后联合时,不会发生排序。

When I do query '$a' or '$b' individually the 'orderBy()' works fine.

当我单独查询 '$a' 或 '$b' 时,'orderBy()' 工作正常。

When I do it in the following way 'orderBy()' happens as a whole.

当我按照以下方式进行时,“orderBy()”会作为一个整体发生。

$a->union($b)
    ->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))
    ->get();

How can I make it so the 'orderBy()' applies for each individually and then union the results back? It seems like it should work.

我怎样才能做到这样“orderBy()”分别适用于每个人,然后将结果合并回来?看起来它应该工作。

EDIT: If anyone can provide a way to do this, even if it's normal MySQL, I will choose yours as the answer as I think there may be a bug with Eloquent.

编辑:如果有人能提供一种方法来做到这一点,即使它是普通的 MySQL,我也会选择你的作为答案,因为我认为 Eloquent 可能存在错误。

回答by Ng Sek Long

The "merge"function in Laravel collection might be able to help you.
The big differnt is that I close off the query with a ->get() in advance, and I use merge() instead of union()

Laravel 集合中的“合并”功能或许可以帮到你。
最大的不同是我提前用 ->get() 关闭了查询,并且我使用了 merge() 而不是 union()

$a = Model::where('code', '=', $code)
->where('col_a', '=' , 1)
->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))->get();

$b = Model::where('code', '=', $code)
->where('col_b', '=' , 1)
->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))->get();

$result = $a->merge($b);

Note: I don't have your data so I can't proof it work, but it least it work on my data so should worth your try

注意:我没有你的数据,所以我无法证明它有效,但至少它适用于我的数据,所以应该值得你尝试

回答by Jaymin Panchal

Just try to apply orderBy()after union()

orderBy()之后尝试申请union()

Try this

尝试这个

$a->union($b)->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))->get();

EDIT

编辑

Researched about and found and prepared eloquent query just try this

研究并发现并准备了雄辩的查询,试试这个

$modelA = Model::where('code', '=', $code)
    ->where('col_a', '=' , 1)
    ->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))

$modelB = Model::where('code', '=', $code)
    ->where('col_b', '=' , 1)
    ->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))

$a = DB::table(DB::raw("({$modelA->toSql()}) as a"))
    ->mergeBindings($modelA->getQuery())
    ->selectRaw("a.*");

$b = DB::table(DB::raw("({$modelB->toSql()}) as b"))
    ->mergeBindings($modelB->getQuery())
    ->selectRaw("b.*");

$a->union($b)->get();

回答by Max Roa

Try the following:

请尝试以下操作:

$a = Model::where('code', '=', $code)
->where('col_a', '=' , 1);

$b = Model::where('code', '=', $code)->where('col_b', '=' , 1)
->union($a)
->get();

$result = $b;

回答by mith

Refer MySql Documentation

参考 MySql文档

Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows. Therefore, the use of ORDER BY in this context is typically in conjunction with LIMIT, so that it is used to determine the subset of the selected rows to retrieve for the SELECT, even though it does not necessarily affect the order of those rows in the final UNION result. If ORDER BY appears without LIMIT in a SELECT, it is optimized away because it will have no effect anyway.

对单个 SELECT 语句使用 ORDER BY 并不意味着行在最终结果中出现的顺序,因为 UNION 默认生成一组无序的行。因此,在此上下文中使用 ORDER BY 通常与 LIMIT 结合使用,以便它用于确定要为 SELECT 检索的所选行的子集,即使它不一定影响这些行在最终的 UNION 结果。如果 ORDER BY 在 SELECT 中没有 LIMIT,它会被优化掉,因为它无论如何都不会产生任何影响。