这是我的查询结果:-

0117491646420160812000000  D   0
0012980013220160911114106  D   0
0020876513320160503000000  E   1
0090022202120161227000000  E   1
0010557700720180504000000  F   2
0020254719320180518000000  F   2

我想要的是:-
0117491646420160812000000  D   0
0012980013220160911114106  D   0
0020876513320160503000000  E   1
0010557700720180504000000  F   2
0090022202120161227000000  E   1
0020254719320180518000000  F   2

我必须按照这样的方式排列我的记录:D总是排在第一位,但在E和F类中,它是以1:1的比例出现的,这意味着E和F是在D之后交替出现的。
SQL查询-
SELECT m.allocationID,
       mt.CatId,
       mt.CatSName,
       r.officerName,
       r.desgName,
       r.grade,
       r.cellNo,
       CASE
           WHEN mt.CatSName = 'D' THEN 0
           WHEN mt.CatSName = 'E' THEN 1
           ELSE 2
       END AS ord
FROM msttransaction m,
     msttemp mt,
     regusers r
WHERE (m.isPending='Y'
       AND m.statusR='Approve'
       AND m.iscancelled='No'
       AND m.allocationID IN
         (SELECT mt.AllocationId
          FROM msttemp
          WHERE mt.quarterId='082505012')
       AND SUBSTRING(m.AllocationId, 1, 11)=r.pfNpsNo)
  OR (m.isPending='N'
      AND m.type='Transit'
      AND m.statusR='Approve'
      AND m.iscancelled='No'
      AND m.allocationID IN
        (SELECT mt.AllocationId
         FROM msttemp
         WHERE mt.quarterId='082505012')
      AND SUBSTRING(m.AllocationId, 1, 11)=r.pfNpsNo)
ORDER BY ord,
         SUBSTRING(m.AllocationId, -14)

注意,我在示例中没有显示某个字段。

最佳答案

你可以用

    ORDER BY FIELD(ord, 'D' , 'E', 'F'), SUBSTRING(m.AllocationId, -14)

07-24 09:15