0

我的表_a:

A|B|C // Columns
3|4|0 // Values

我想先按小值进行选择排序。像这样的东西:

"SELECT * FROM table_a ORDER BY (smallest of A,B,C first, second and third (Bigger one)) ASC"

returns: C=0,A=3,B=4

那可能吗?不使用 PHP 或其他脚本?

4

1 回答 1

0

这是一种快速而肮脏的尝试,但似乎得到了预期的结果,但不完全确定您为什么要这样做。 http://sqlfiddle.com/#!2/1d3ec/2

SELECT
    l2.A,
    l2.B,
    l2.C
FROM (
    SELECT
        l1.A,
        l1.B,
        l1.C,
        l1.smallestColumn,
        l1.largestColumn,
        CASE
            WHEN l1.A > l1.smallestColumn AND l1.A < l1.largestColumn THEN l1.A
            WHEN l1.B > l1.smallestColumn AND l1.B < l1.largestColumn THEN l1.B
            ELSE l1.C -- Fallback to C, in case of a tied result
        END AS middleColumn
    FROM (
        SELECT
            A,
            B,
            C,
            LEAST( A , B , C ) AS smallestColumn,
            GREATEST( A , B , C ) AS largestColumn
        FROM table_a
    ) l1
) l2
ORDER BY
    l2.smallestColumn,
    l2.middleColumn,
    l2.largestColumn;
于 2013-06-11T17:46:19.127 回答