PostgreSQL incorrect sorting

后端 未结 2 1027
甜味超标
甜味超标 2020-12-09 18:25

I use PostgreSQL 9.3.3 and I have a table with one column named as title (character varying(50)).

When I have executed the following query:

select *         


        
2条回答
  •  悲哀的现实
    2020-12-09 19:31

    Sort behaviour for text (including char and varchar as well as the text type) depends on the current collation of your locale.

    See previous closely related questions:

    • PostgreSQL Sort
    • https://stackoverflow.com/q/21006868/398670

    If you want to do a simplistic sort by ASCII value, rather than a properly localized sort following your local language rules, you can use the COLLATE clause

    select * 
    from test
    order by title COLLATE "C" ASC
    

    or change the database collation globally (requires dump and reload, or full reindex). On my Fedora 19 Linux system, I get the following results:

    regress=> SHOW lc_collate;
     lc_collate  
    -------------
     en_US.UTF-8
    (1 row)
    
    regress=> WITH v(title) AS (VALUES ('#a'), ('a'), ('#'), ('a#a'), ('a#')) 
              SELECT title FROM v ORDER BY title ASC;
     title 
    -------
     #
     a
     #a
     a#
     a#a
    (5 rows)
    
    regress=> WITH v(title) AS (VALUES ('#a'), ('a'), ('#'), ('a#a'), ('a#')) 
              SELECT title FROM v ORDER BY title COLLATE "C" ASC;
     title 
    -------
     #
     #a
     a
     a#
     a#a
    (5 rows)
    

    PostgreSQL uses your operating system's collation support, so it's possible for results to vary slightly from host OS to host OS. In particular, at least some versions of Mac OS X have significantly broken unicode collation handling.

提交回复
热议问题