postgres column alias problem

后端 未结 4 738
余生分开走
余生分开走 2020-12-06 11:29

As a newbie to Postgresql (I\'m moving over because I\'m moving my site to heroku who only support it, I\'m having to refactor some of my queries and code. Here\'s a problem

4条回答
  •  甜味超标
    2020-12-06 11:52

    In PostgreSQL you can not use expression with an alias in order by. Only plain aliases work there. Your query should look like this:

       select distinct 
              l2.*, 
              l.user_id as l_user_id, 
              l.geopoint_id as l_geopoint_id 
         from locations l 
    left join locations l2 on l.geopoint_id = l2.geopoint_id 
        where l.user_id = 8 
     order by l2.geopoint_id, l.user_id = l2.user_id desc;
    

    I assume you mean that l2.user_id=l.user_id ought to go first.

    This is relevant message on PostgreSQL-general mailing list. The following is in the documentation of ORDER BY clause:

    Each expression can be the name or ordinal number of an output column (SELECT list item), or it can be an arbitrary expression formed from input-column values.

    So no aliases when expression used.

提交回复
热议问题