【发布时间】:2019-03-24 18:34:43
【问题描述】:
Postgres order by 得到错误的结果:
postgres=# SELECT (url) FROM posts_post ORDER BY url;
url
--------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
http://nautil.us/issue/70/variables/aging-is-a-communication-breakdown
https://github.com/felixse/FluentTerminal
http://www.bbc.com/future/story/20160408-the-ancient-peruvian-mystery-solved-from-space
http://www.graffathon.fi/2016/presentations/additive_slides.pdf
(4 rows)
如您所见,“http://nautil.us/issue/70/variables/aging-is-a-communication-breakdown”有问题。它没有正确排序。
我使用 Python 和 psycopg2 将解析结果保存在 Postgres 中,然后指出,我无法测试排序,导致 Postgres return order by 出现错误。
更新:复制:
CREATE TABLE test_post ("id" serial NOT NULL PRIMARY KEY, "title" text NOT NULL, "url" text NOT NULL, "created" timestamp with time zone NOT NULL);
INSERT INTO test_post (title, url, created) VALUES ('Aging Is', 'http://nautil.us/issue/70/variables/aging-is-a-communication-breakdown', NOW()) ON CONFLICT DO NOTHING;
INSERT INTO test_post (title, url, created) VALUES ('Untrusted – a user', 'https://github.com/felixse/FluentTerminal', NOW()) ON CONFLICT DO NOTHING;
INSERT INTO test_post (title, url, created) VALUES ('Artyping (1939)', 'http://www.bbc.com/future/story/20160408-the-ancient-peruvian-mystery-solved-from-space', NOW()) ON CONFLICT DO NOTHING;
INSERT INTO test_post (title, url, created) VALUES (' Applying the Universal', 'http://www.graffathon.fi/2016/presentations/additive_slides.pdf', NOW()) ON CONFLICT DO NOTHING;
SELECT (url) FROM test_post ORDER BY url;
x86_64-pc-linux-gnu 上的 PostgreSQL 11.2 (Debian 11.2-1.pgdg90+1),由 gcc (Debian 6.3.0-18+deb9u) 编译 1) 6.3.0 20170516, 64位
【问题讨论】:
-
":"小于"s"所以它排序正确(http: 应该在所有 https: 之前)。 -
@thebjorn 一开始我也这么认为,但是底部有两行以“http”开头。
-
编辑您的问题,并粘贴
create table、insert和select语句来重现您的问题。 -
标记 Postgres 版本。使用您的复制有限集显示结果(所以 4 行而不是 33 行)。无论如何,对我有用,Postgres 11.1。
-
排序没有问题:有些排序规则会忽略非字母或数字的字符。