ORACLE表中的以下结构:
FILE_NAME
-----------
12345_l.tif
12345_m.tif
12345_r.tif
12345_x.tif
12345_y.tif
需要以下结果:
First *_m*
Then *_l*
Then *_r*
Then * (everything else)
尝试:
SELECT FILE_NAME FROM TABLE
WHERE FILE_NAME LIKE '12345%'
ORDER BY regexp_replace(FILE_NAME, '_m', '_1'),
regexp_replace(FILE_NAME, '_l', '_2'),
regexp_replace(FILE_NAME, '_r', '_3')
但这给了我一个错误的结果。
任何有提示的人?
TIA Matt
答案 0 :(得分:2)
将您的ORDER BY更改为按数字排序:
ORDER BY regexp_replace(FILE_NAME, '_m', 1),
regexp_replace(FILE_NAME, '_l', 2),
regexp_replace(FILE_NAME, '_r', 3);
e.g。
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);
给出:
==============
12345_m.tif
12345_l.tif
12345_r.tif
12345_x.tif
12345_y.tif
希望它有所帮助...
或者你可以使用:
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;
E.G:
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;
给出:
12345_m.tif
12345_l.tif
12345_r.tif
12345_x.tif
12345_y.tif