使用 Laravel 4.1 对 UNION 查询进行排序

Sorting UNION queries with Laravel 4.1(使用 Laravel 4.1 对 UNION 查询进行排序)
本文介绍了使用 Laravel 4.1 对 UNION 查询进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我认为 Laravel 4 和 Laravel 4.1 之间的 union 发生了一些变化.我有 2 个模型.

I think there is something changed in the union between Laravel 4 and Laravel 4.1. I have 2 models.

$photos = DB::table('photos')->select('id', 'name', 'created_at');
$videos = DB::table('videos')->select('id', 'name', 'created_at');

我想合并 2 个查询并使用 created_at 字段对 2 个查询进行排序.

I want to union the 2 querys and order the 2 querys with the created_at field.

$photos = $photos->orderBy('created_at', 'desc');
$combined = $photos->union($videos);

使用 Laravel 4 它给了我这个查询:

With Laravel 4 it gives me this query:

select `id`, `name`, `created_at` from `videos`
union
select `id`, `name`, `created_at` from `photos`
order by `created_at` desc

这工作正常,它将两个查询的结果排序在一起.在 Laravel 4.1 中,它给了我这个查询:

This works ok, it sorts the results for both querys together. In Laravel 4.1 it gives me this query:

(select `id`, `name`, `created_at` from `videos`)
union
(select `id`, `name`, `created_at` from `photos` order by `created_at` desc)

这会产生一个视频列表,然后是一个有序的照片列表.我需要有一个列表,其中对组合查询进行了排序.我想让 Laravel 给我这个查询:

This results in a list of videos and after that an ordered list of photos. I need to have a list where the to combined querys are sorted. I want Laravel to give me this query:

(select `id`, `name`, `created_at` from `videos`)
union
(select `id`, `name`, `created_at` from `photos`)
order by `created_at` desc

如何在 Laravel 中实现此功能?

How do get this working in Laravel?

推荐答案

似乎在这个 pull request 中修复了:https://github.com/laravel/framework/pull/3901

It seems to be fixed in this pull request: https://github.com/laravel/framework/pull/3901

这篇关于使用 Laravel 4.1 对 UNION 查询进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?(警告:mysqli_query() 需要至少 2 个参数,1 个给定.什么?)
INSERT query produces quot;Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean givenquot;(INSERT 查询产生“警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,给出布尔值;)
prepared statements - are they necessary(准备好的陈述 - 它们是否必要)
Do I need to escape my variables if I use MySQLi prepared statements?(如果我使用 MySQLi 准备好的语句,是否需要转义我的变量?)
Properly Escaping with MySQLI | query over prepared statements(使用 MySQLI 正确转义 |查询准备好的语句)
Is it possible to use mysqli_fetch_object with a prepared statement(是否可以将 mysqli_fetch_object 与准备好的语句一起使用)