2015-10-31 108 views
-2

我有這個表編寫SQL查詢來顯示結果中特定的順序

+----+--------+------------+-----------+ 
| Id | day_id | subject_id | period_Id | 
+----+--------+------------+-----------+ 
| 1 | 1  | 1   | 1   | 
| 2 | 1  | 2   | 2   | 
| 8 | 2  | 6   | 1   | 
| 9 | 2  | 7   | 2   | 
| 15 | 3  | 3   | 1   | 
| 16 | 3  | 4   | 2   | 
| 22 | 4  | 5   | 1   | 
| 23 | 4  | 5   | 2   | 
| 24 | 4  | 6   | 3   | 
| 29 | 5  | 8   | 1   | 
| 30 | 5  | 1   | 2   | 


to something like this 



| Id | day_id | subject_id | period_Id | 
| 1 | 1  | 1   | 1   | 
| 8 | 2  | 6   | 1   | 
| 15 | 3  | 3   | 1   | 
| 22 | 4  | 5   | 1   | 
| 29 | 5  | 8   | 1   | 
| 2 | 1  | 2   | 2   | 
| 2 | 1  | 2   | 2   | 
| 16 | 3  | 4   | 2   | 
| 23 | 4  | 5   | 2   | 
| 30 | 5  | 1   | 2   | 
+----+--------+------------+-----------+ 

所以,我要選擇的每一天,做這週數一個週期有不同的主題。所以第一個主題不會出現,直到所有主題都被選中。

回答

0

可以ORDER BY period_id,然後再由day_id

SELECT * 
FROM your_table 
ORDER BY period_Id, day_Id 

LiveDemo

+0

但對於下週...天階1 2 3 4 5然後1 2 3 4 5然後1 2 3 4 5 –

+0

@NaifAlblawi下週擴展您的數據並顯示所需的輸出。無論如何,你已經有一天的訂單 – lad2025