mysql 的复杂顺序
Complex order by mysql
如果我有这样的table
+----+----------------+
| id | id_alternativo |
+----+----------------+
| 15 | 18 |
+----+----------------+
| 16 | 0 |
+----+----------------+
| 17 | 0 |
+----+----------------+
| 18 | 0 |
+----+----------------+
如何让记录在 ID 15 之后显示 ID 18?
据我所知,只需使用 order by id (18, 15)
和您需要的任何其他 ID。
select id,id_alternativo
from table_name
order by case id when 15 Then 1
when 18 Then 2
when 16 Then 3
when 17 Then 4
else 5
end;
你可以这样使用命令:
SELECT id
FROM mytab
ORDER BY IF(id=18,155,id*10);
样本
MariaDB []> select id from mytab;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
+----+
15 rows in set (0.00 sec)
MariaDB []> SELECT id
-> FROM mytab
-> ORDER BY IF(id=18,155,id*10);
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 18 |
| 16 |
| 17 |
| 19 |
+----+
15 rows in set (0.00 sec)
MariaDB []>
查看 MySQL 文档,您可以在排序中使用多列并每列使用 DESC/ASC。
http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
如果我理解正确,您的查询将如下所示:
SELECT id, id_alternativo FROM table ORDER BY id_alternativo DESC, id DESC;
+------+----------------+
| id | id_alternativo |
+------+----------------+
| 15 | 18 |
| 18 | 0 |
| 17 | 0 |
| 16 | 0 |
+------+----------------+
一个解决方案是使用这样的自连接:
select t.*
from
yourtable t left join yourtable o
on t.id = o.id_alternativo
order by
coalesce(o.id, t.id), t.id
这会将所有备用 ID 放在主要 ID 之后(在本例中,18 将跟在 15 之后)。
请看一个fiddlehere。请注意,除非替代 ID 有另一个替代 ID(例如,如果 18 本身有另一个替代 ID),这将起作用,但这不能纯粹用 MySQL 解决,因为它还不支持递归查询。
抱歉,我使用了一个简单的示例,但我的 table 更复杂。 id_alternativo 可以是递归的(id 18 可以有一个 id_alternativo = 19)等等,并且 id_alternativo 不能是 table 上最高的 id 所以 ORDER BY id_alternativo DESC, id DESC
不起作用。这是我的 table:
的查询
SELECT
a.id, a.compatibile, a.id_alternativo
FROM
ordini_righe AS a
WHERE
intestazione IN (398010) AND a.canc = 0
AND a.stato_ordine = 0
这是结果
+-------+-------------+----------------+
| id | compatibile | id_alternativo |
+-------+-------------+----------------+
|828924 | 0 | 828931 |
+-------+-------------+----------------+
|828925 | 828932 | 0 |
+-------+-------------+----------------+
|828926 | 0 | 0 |
+-------+-------------+----------------+
|828927 | 0 | 0 |
+-------+-------------+----------------+
|828931 | 0 | 828933 |
+-------+-------------+----------------+
|828932 | 828932 | 0 |
+-------+-------------+----------------+
|828933 | 0 | 0 |
+-------+-------------+----------------+
我不得不订购compatibile desc,然后通过id_alternativo 和id 之间的关系订购其他记录。所以我解决了使用像这样的新列
SELECT
a.id, a.compatibile, a.id_alternativo, IF(id_alternativo = 0, a.id, id_alternativo) ordine
FROM
ordini_righe AS a
JOIN
locazioni AS b ON a.locazione = b.id
JOIN
stati_righe AS c ON a.stato_ordine = c.id
WHERE
intestazione IN (398010) AND a.canc = 0
AND a.stato_ordine = 0
ORDER BY compatibile DESC, ordine, a.id ASC
我得到了想要的结果
+-------+-------------+----------------+--------+
| id | compatibile | id_alternativo | ordine |
+-------+-------------+----------------+--------+
|828925 | 828932 | 828931 | 828925 |
+-------+-------------+----------------+--------+
|828932 | 828932 | 0 | 828932 |
+-------+-------------+----------------+--------+
|828926 | 0 | 0 | 828926 |
+-------+-------------+----------------+--------+
|828927 | 0 | 0 | 828927 |
+-------+-------------+----------------+--------+
|828924 | 0 | 828931 | 828931 |
+-------+-------------+----------------+--------+
|828931 | 0 | 828933 | 828933 |
+-------+-------------+----------------+--------+
|828933 | 0 | 0 | 828933 |
+-------+-------------+----------------+--------+
如果我有这样的table
+----+----------------+
| id | id_alternativo |
+----+----------------+
| 15 | 18 |
+----+----------------+
| 16 | 0 |
+----+----------------+
| 17 | 0 |
+----+----------------+
| 18 | 0 |
+----+----------------+
如何让记录在 ID 15 之后显示 ID 18?
据我所知,只需使用 order by id (18, 15)
和您需要的任何其他 ID。
select id,id_alternativo
from table_name
order by case id when 15 Then 1
when 18 Then 2
when 16 Then 3
when 17 Then 4
else 5
end;
你可以这样使用命令:
SELECT id
FROM mytab
ORDER BY IF(id=18,155,id*10);
样本
MariaDB []> select id from mytab;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
+----+
15 rows in set (0.00 sec)
MariaDB []> SELECT id
-> FROM mytab
-> ORDER BY IF(id=18,155,id*10);
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 18 |
| 16 |
| 17 |
| 19 |
+----+
15 rows in set (0.00 sec)
MariaDB []>
查看 MySQL 文档,您可以在排序中使用多列并每列使用 DESC/ASC。
http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
如果我理解正确,您的查询将如下所示:
SELECT id, id_alternativo FROM table ORDER BY id_alternativo DESC, id DESC;
+------+----------------+
| id | id_alternativo |
+------+----------------+
| 15 | 18 |
| 18 | 0 |
| 17 | 0 |
| 16 | 0 |
+------+----------------+
一个解决方案是使用这样的自连接:
select t.*
from
yourtable t left join yourtable o
on t.id = o.id_alternativo
order by
coalesce(o.id, t.id), t.id
这会将所有备用 ID 放在主要 ID 之后(在本例中,18 将跟在 15 之后)。
请看一个fiddlehere。请注意,除非替代 ID 有另一个替代 ID(例如,如果 18 本身有另一个替代 ID),这将起作用,但这不能纯粹用 MySQL 解决,因为它还不支持递归查询。
抱歉,我使用了一个简单的示例,但我的 table 更复杂。 id_alternativo 可以是递归的(id 18 可以有一个 id_alternativo = 19)等等,并且 id_alternativo 不能是 table 上最高的 id 所以 ORDER BY id_alternativo DESC, id DESC
不起作用。这是我的 table:
SELECT
a.id, a.compatibile, a.id_alternativo
FROM
ordini_righe AS a
WHERE
intestazione IN (398010) AND a.canc = 0
AND a.stato_ordine = 0
这是结果
+-------+-------------+----------------+
| id | compatibile | id_alternativo |
+-------+-------------+----------------+
|828924 | 0 | 828931 |
+-------+-------------+----------------+
|828925 | 828932 | 0 |
+-------+-------------+----------------+
|828926 | 0 | 0 |
+-------+-------------+----------------+
|828927 | 0 | 0 |
+-------+-------------+----------------+
|828931 | 0 | 828933 |
+-------+-------------+----------------+
|828932 | 828932 | 0 |
+-------+-------------+----------------+
|828933 | 0 | 0 |
+-------+-------------+----------------+
我不得不订购compatibile desc,然后通过id_alternativo 和id 之间的关系订购其他记录。所以我解决了使用像这样的新列
SELECT
a.id, a.compatibile, a.id_alternativo, IF(id_alternativo = 0, a.id, id_alternativo) ordine
FROM
ordini_righe AS a
JOIN
locazioni AS b ON a.locazione = b.id
JOIN
stati_righe AS c ON a.stato_ordine = c.id
WHERE
intestazione IN (398010) AND a.canc = 0
AND a.stato_ordine = 0
ORDER BY compatibile DESC, ordine, a.id ASC
我得到了想要的结果
+-------+-------------+----------------+--------+
| id | compatibile | id_alternativo | ordine |
+-------+-------------+----------------+--------+
|828925 | 828932 | 828931 | 828925 |
+-------+-------------+----------------+--------+
|828932 | 828932 | 0 | 828932 |
+-------+-------------+----------------+--------+
|828926 | 0 | 0 | 828926 |
+-------+-------------+----------------+--------+
|828927 | 0 | 0 | 828927 |
+-------+-------------+----------------+--------+
|828924 | 0 | 828931 | 828931 |
+-------+-------------+----------------+--------+
|828931 | 0 | 828933 | 828933 |
+-------+-------------+----------------+--------+
|828933 | 0 | 0 | 828933 |
+-------+-------------+----------------+--------+