2016-04-28 62 views
0

,如果我有這樣的複雜的順序由MySQL

+----+----------------+ 
| id | id_alternativo | 
+----+----------------+ 
| 15 |  18  |  
+----+----------------+ 
| 16 |  0  |  
+----+----------------+ 
| 17 |  0  |  
+----+----------------+ 
| 18 |  0  | 
+----+----------------+ 

如何訂購的記錄編號15後,出示身份證18表?

+0

你想靜態排序? –

+0

靜態排序是什麼意思? – andyts93

+0

如何分類記錄? – Sadikhasan

回答

0

對不起,我用了一個簡單的例子,但我的表更復雜。 id_alternativo可以遞歸(id 18可以有一個id_alternativo = 19)等等,id_alternativo不能是表上最高的ID,所以ORDER BY id_alternativo DESC, id DESC不起作用。下面是我的表的查詢:

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說明,然後由beetween 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 | 
+-------+-------------+----------------+--------+ 
0

從我的理解只是使用order by id (18, 15)和任何其他你需要的ID。

0
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; 
0

你可以使用順序是這樣的:

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 []> 
3

看着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 | 
+------+----------------+ 
+0

我認爲OP希望所有替代id後面的主ID(15,然後18,因爲是15替代id,然後16,17等) – fthiella

1

一種解決方案是使用自加入這樣的:

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)。

請參閱小提琴here。請注意,這將工作,除非替代ID有另一個替代ID(例如,如果18本身有另一個替代ID),但這不能純粹用MySQL解決,因爲它不支持遞歸查詢。

+0

謝謝,您的解決方案工作,如果替代ID沒有替代ID本身,但在我的情況下,它可以。順便說一句我找到了一個解決方案,我發佈了我的答案:) – andyts93