使用Order by的别名

时间:2016-04-21 12:52:07

标签: sql postgresql

我有一个问题:

SELECT distinct(a0.ID,a0.str)  
from sev_modul_4_type1 as a0 
where ((lower(a0.str) LIKE '%а%'))

我想通过str订购它,但如果这样做的话

select * from (
   SELECT distinct(a0.ID,a0.str)  
   from sev_modul_4_type1 as a0 
   where ((lower(a0.str) LIKE '%а%'))
) x order by str desc

我收到错误:

ERROR:  column "str" does not exist
LINE 1: ...s a0 where ((lower(a0.str) LIKE '%а%'))) x order by str desc
                                                         ^

即使我使用sev_modul_4_type1.stra0.str

我该怎么做?

我也试过了order by 2 desc,但它给了我同样的错误,只有order by 1 desc(按ID排序)

2 个答案:

答案 0 :(得分:1)

试试这种方式

select * from (
   SELECT distinct a0.ID,
                a0.str  
   from sev_modul_4_type1 as a0 
   where ((lower(a0.str) LIKE '%а%'))
) x order by str desc

答案 1 :(得分:1)

select * from (
   SELECT distinct a0.ID, a0.str  
   from sev_modul_4_type1 as a0 
   where ((lower(a0.str) LIKE '%а%'))
) x order by x.str desc

尝试从distinct中删除括号,然后使用ORDER BY子句中的别名。