sql >> Database teknologi >  >> RDS >> Oracle

ORACLE SQL | Ændring af data i ORDER BY

Skift din ORDER BY for at bestille den efter et numerisk:

 ORDER BY regexp_replace(FILE_NAME, '_m', 1),
          regexp_replace(FILE_NAME, '_l', 2),
          regexp_replace(FILE_NAME, '_r', 3);

f.eks.

WITH t
  AS (SELECT '12345_l.tif' AS file_name FROM dual
      UNION
      SELECT '12345_m.tif' FROM dual
      UNION
      SELECT '12345_r.tif' FROM dual
      UNION
      SELECT '12345_x.tif' FROM dual
      UNION
      SELECT '12345_y.tif' FROM dual)
SELECT file_name
  FROM t
 ORDER BY regexp_replace(FILE_NAME, '_m', 1),
          regexp_replace(FILE_NAME, '_l', 2),
          regexp_replace(FILE_NAME, '_r', 3);

Giver:

==============
12345_m.tif
12345_l.tif
12345_r.tif
12345_x.tif
12345_y.tif

Håber det hjælper...

Alternativt kan du bruge:

 ORDER BY (CASE SUBSTR(file_name, INSTR(file_name, '_')+1, 1)
             WHEN 'm' THEN 1
             WHEN 'l' THEN 2
             WHEN 'r' THEN 3
             ELSE 4
            END) ASC;

F.eks.:

WITH t
  AS (SELECT '12345_l.tif' AS file_name FROM dual
      UNION
      SELECT '12345_y.tif' FROM dual
      UNION
      SELECT '12345_r.tif' FROM dual
      UNION
      SELECT '12345_x.tif' FROM dual
      UNION
      SELECT '12345_m.tif' FROM dual)
SELECT file_name
  FROM t
 ORDER BY (CASE SUBSTR(file_name, INSTR(file_name, '_')+1, 1)
             WHEN 'm' THEN 1
             WHEN 'l' THEN 2
             WHEN 'r' THEN 3
             ELSE 4
            END) ASC;

Giver:

12345_m.tif
12345_l.tif
12345_r.tif
12345_x.tif
12345_y.tif



  1. Sådan omdannes en database i MySQL Workbench

  2. oprette milliontabel i en database?

  3. sammenføjning af to borde med flere forhold i skinner

  4. Forklar plan i mysql ydeevne ved hjælp af Using temporary; Brug af filesort; Brug af indekstilstand