【发布时间】:2021-05-28 21:10:21
【问题描述】:
我有一个数据库,我需要按照与表中填充的顺序相同的顺序检索数据。表名是圣经当我在 psql 中输入table bible; 时,它会按照填充顺序打印数据,但是当我尝试检索它时,某些行总是乱序的,如下例所示:
table bible
-[ RECORD 1 ]-----------------------------------------------------------------------------------------------------------------------------------------
id | 1
day | 1
book | Genesis
chapter | 1
verse | 1
text | In the beginning God created the heavens and the earth.
link | https://api.biblia.com/v1/bible/content/asv.txt.txt?passage=Genesis1.1&key=dc5e2d416f46150bf6ceb21d884b644f
-[ RECORD 2 ]-----------------------------------------------------------------------------------------------------------------------------------------
id | 2
day | 1
book | John
chapter | 1
verse | 1
text | In the beginning was the Word, and the Word was with God, and the Word was God.
link | https://api.biblia.com/v1/bible/content/asv.txt.txt?passage=John1.1&key=dc5e2d416f46150bf6ceb21d884b644f
-[ RECORD 3 ]-----------------------------------------------------------------------------------------------------------------------------------------
id | 3
day | 1
book | John
chapter | 1
verse | 2
text | The same was in the beginning with God.
link | https://api.biblia.com/v1/bible/content/asv.txt.txt?passage=John1.2&key=dc5e2d416f46150bf6ceb21d884b644f
一切都井井有条,但是当我尝试使用例如select * from bible where day='1' 或select * from bible where day='1' order by day 或select * from bible where day='1' order by day, id; 查询相同的内容时,我总是在选择的那一天(此处为 1)中出现一些乱序的行或其他任何一天。
我一直在使用 Django 来干扰 Postgres 数据库,但是自从我发现这个问题后,我尝试使用 SQL 进行查询,但是什么也没有,我仍然得到乱序的行,尽管它们都有唯一的 ID,我用 select count(distinct id), count(id) from bible;
- [ RECORD 1 ]------------------------------------------------------------------------------------------------------
id | 1
day | 1
book | Genesis
chapter | 1
verse | 1
text | In the beginning God created the heavens and the earth.
link | https://api.biblia.com/v1/bible/content/asv.txt.txt?passage=Genesis1.1&key=dc5e2d416f46150bf6ceb21d884b644f
-[ RECORD 2 ]-----------------------------------------------------------------------------------------------------------------------------------------
id | 10
day | 1
book | Colossians
chapter | 1
verse | 18
text | And he is the head of the body, the church: who is the beginning, the firstborn from the dead; that in all things he might have the preemine
nce.
link | https://api.biblia.com/v1/bible/content/asv.txt.txt?passage=Colossians1.18&key=dc5e2d416f46150bf6ceb21d884b644f
-[ RECORD 3 ]-----------------------------------------------------------------------------------------------------------------------------------------
id | 11
day | 1
book | Genesis
chapter | 1
verse | 2
text | And the earth was waste and void; and darkness was upon the face of the deep: and the Spirit of God moved upon the face of the waters.
link | https://api.biblia.com/v1/bible/content/asv.txt.txt?passage=Genesis1.2&key=dc5e2d416f46150bf6ceb21d884b644f
正如您在上面看到的,如果您注意到,ID 的顺序是 1、10、11。
我的桌子
Table "public.bible";
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
---------+------+-----------+----------+---------+----------+--------------+-------------
id | text | | | | extended | |
day | text | | | | extended | |
book | text | | | | extended | |
chapter | text | | | | extended | |
verse | text | | | | extended | |
text | text | | | | extended | |
link | text | | | | extended | |
Access method: heap
id 字段是文本类型,因为我使用 pandas 的 to_sql() 方法来填充圣经表。我尝试删除 id 列,然后使用 ALTER TABLE bible ADD COLUMN id SERIAL PRIMARY KEY; 再次将其添加为 PK,但我仍然无法正常返回数据。
无论如何,我是否可以通过 id 排序来检索数据,而不会使某些行完全乱序?提前谢谢!
【问题讨论】:
-
你有索引吗?
-
@sddk 啊,没有索引
标签: sql django database postgresql psql