gpt4 book ai didi

postgresql - Postgres 排序顺序错误

转载 作者:行者123 更新时间:2023-11-29 12:01:46 32 4
gpt4 key购买 nike

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 返回 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+deb9u1) 6.3.0 20170516,64位

最佳答案

假设您使用 UTF8 编码,指定排序规则而不是接受默认排序规则应该可以解决您眼前的问题。这是否正确取决于应用程序。

有几种不同的方法来指定排序规则。您可以在数据库集群初始化、创建数据库、运行查询等时指定它。请参阅 Collation support在文档中了解更多详细信息。

CREATE TABLE test_post (
"id" serial NOT NULL PRIMARY KEY,
"title" text NOT NULL,
"url" text collate ucs_basic 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;

http://nautil.us/issue/70/variables/aging-is-a-communication-breakdown
http://www.bbc.com/future/story/20160408-the-ancient-peruvian-mystery-solved-from-space
http://www.graffathon.fi/2016/presentations/additive_slides.pdf
https://github.com/felixse/FluentTerminal

关于postgresql - Postgres 排序顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55327146/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com