postgres忽视" - "排序时

时间:2015-03-12 07:21:43

标签: postgresql

我对值进行排序。 Postgres忽略了" - "。这是我的疑问:

select 0 as key, 
       '------ select ------' as value 
union 
SELECT contact_replica_child.contact_id as key,
       contact_replica_child.last_name||', '||contact_replica_child.first_name as value 
FROM contact_replica_child 
  join listing_replica_child on contact_replica_child.administrative_agency_id = listing_replica_child.agency_id 
where listing_replica_child.session_id = '3edfa73687a53604a50708d3d5d90221' 
order by value ;

我得到了这个:

  key   |          value          
--------+-------------------------
 581489 | Contact, Administrative
 581490 | Green, Kelley
      0 | ------ select ------

我期待着这个:

  key   |          value          
--------+-------------------------
      0 | ------ select ------
 581489 | Contact, Administrative
 581490 | Green, Kelley

任何解决方案?

1 个答案:

答案 0 :(得分:1)

虽然我必须承认我不明白为什么Postgres采用这种方式行事,你可以通过使用括号将order by子句限制为第二个查询来轻松解决它:

SELECT    0 AS key, '------ select ------' AS value 
UNION ALL
(SELECT   contact_replica_child.contact_id AS key,
          contact_replica_child.last_name || ',' ||  contact_replica_child.first_name AS value 
 FROM     contact_replica_child 
 JOIN     listing_replica_child ON 
          contact_replica_child.administrative_agency_id = 
          listing_replica_child.agency_id
 WHERE    listing_replica_child.session_id = '3edfa73687a53604a50708d3d5d90221' 
 ORDER BY value
);