MYSQL联盟订购 [英] MYSQL UNION ORDERING

查看:77
本文介绍了MYSQL联盟订购的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对第一个查询进行排序并保留返回的行作为第一行,而不对第二个查询进行排序. (如果有道理)

Is it possible to order the first enquiry and keep the rows returned as first and not order the second enquiry. (If that makes sence)

我当前查询的一个示例是:

An example of my current enquiry is :

SELECT
    *
FROM 
    Devices
WHERE
    Live = 'true'
    AND Category = 'apple'
ORDER BY
    ListOrder
UNION
SELECT
        *
    FROM
        Devices
    WHERE
        DeviceLive = 'true'

我希望apple类别下的设备将按照该列表的顺序进行组织,并位于其他设备上方的列表顶部.但这似乎使两个查询混杂在一起.

I was hoping that the devices under the category apple would be organised in there list order and would be at the top of the list above the other devices. But this seems to jumble the two querys together.

推荐答案

您需要引入一个人工排序键.像这样:

You'd need to introduce an artificial sort key. Something like:

SELECT
    *, 1 as SortKey
FROM 
    Devices
WHERE
    Live = 'true'
    AND Category = 'apple'
UNION
SELECT
        *, 2 as SortKey
    FROM
        Devices
    WHERE
        DeviceLive = 'true'
ORDER BY SortKey, ListOrder

这篇关于MYSQL联盟订购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆