Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql按顺序排序_Sql_Django_Database_Postgresql_Psql - Fatal编程技术网

Postgresql按顺序排序

Postgresql按顺序排序,sql,django,database,postgresql,psql,Sql,Django,Database,Postgresql,Psql,我有一个数据库,需要在其中检索数据,其顺序与表中填充的顺序相同。当我输入表格bible时,表格名称是bible在psql中,它按填充的顺序打印数据,但当我尝试检索数据时,某些行总是出现顺序错误,如以下示例所示: 表格圣经 -[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------------------

我有一个数据库,需要在其中检索数据,其顺序与表中填充的顺序相同。当我输入
表格bible时,表格名称是bible
在psql中,它按填充的顺序打印数据,但当我尝试检索数据时,某些行总是出现顺序错误,如以下示例所示:

表格圣经

-[ 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'按日排序
select*from bible where day='1'按日排序,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字段是text类型,因为我使用pandas的
to_sql()
方法填充bible表。我试着删除id列,然后用
altertable bible ADD column id SERIAL主键再次添加它作为主键但我仍然无法按顺序返回数据


无论如何,我是否可以检索id为的有序数据,而不会使某些行完全无序?提前谢谢你

您应
您的
id
转换为整数,以将其排序为数字

SELECT * FROM bible ORDER BY cast(id AS integer);

尽管@jordanvrtanoski是正确的,但django的做法是:

>>> Bible.objects.extra(select={'id': 'CAST(id AS INTEGER)'}).order_by('id').values('id')
<QuerySet [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 10}, {'id': 20}]>
否则就会出现这个问题:(注意
1
后面跟着
10
,而不是
2

>>圣经.对象.按('id')排序.值('id'))

我强烈建议您不要执行任何操作,并正确设置表(具有正确的列类型,而不是将所有内容都设置为
text
),否则您的查询性能会很差。。BIG TIME

基于@jordanvrtanoski和@Javier Buzzi的两个答案,以及一些在线搜索,问题在于id的类型为TEXT(或VARCHAR),因此,您需要将id强制转换为INTEGER,如下所示:

ALTER TABLE bible ALTER COLUMN id TYPE integer使用(id::integer)

这是我的桌子

                                                Table "public.bible"
Column  |  Type   | Collation | Nullable |                 Default                 | Storage  | Stats target | Description 
---------+---------+-----------+----------+-----------------------------------------+----------+--------------+-------------
id      | integer |           |          | nextval('bible_id_seq'::regclass) | plain    |              | 
day     | text    |           |          |                                         | extended |              | 
book    | text    |           |          |                                         | extended |              | 
chapter | text    |           |          |                                         | extended |              | 
verse   | text    |           |          |                                         | extended |              | 
text    | text    |           |          |                                         | extended |              | 
link    | text    |           |          |                                         | extended |              | 
Indexes:
    "lesson_unique_id" UNIQUE CONSTRAINT, btree (id)
Referenced by:
    TABLE "notes_note" CONSTRAINT "notes_note_verse_id_5586a4bf_fk" FOREIGN KEY (verse_id) REFERENCES days_lesson(id) DEFERRABLE INITIALLY DEFERRED
Access method: heap

希望这能帮助其他人,谢谢大家

你有什么索引吗?@sddk啊,没有索引显示我可以付钱给你,好先生?谢谢你的回复!但是,我在对象上使用filter,就像在SQL行(
…WHERE day='1'
)中一样,所以当我尝试将额外的方法附加到筛选结果时,我得到了一个ID对象的查询集,就像在您的第二个代码段中一样,您知道在Bible.objects.filter(day=1)上应用select方法的方法吗。。。?谢谢,我已经编辑了你的答案,所以queryset会像SQL等价物一样返回有序对象。谢谢你的@JavierBuzzi@MurphyAdam为你更新了我的答案我已经按照你的例子成功了;最后是
Bible.objects.filter(day=day).extra({'id':“CAST(id as INTEGER)”).order_by('id')
Ah,对不起,我已经发布了一个答案,你是对的,干扰的是我在模型上设置了一个id字段,主键=True,它抛出了一个错误,比如“id是不明确的”,所以我刚刚删除了id(我在试图修复错误时忘记了它),进行了迁移,一切都很好,我现在也不依赖Django的额外方法,因为正如您所提到的,这不是最好的方法,所以我将表上的id字段转换为整数。谢谢您的帮助!!
>>> Bible.objects.order_by('id').values('id')
<QuerySet [{'id': '1'}, {'id': '10'}, {'id': '2'}, {'id': '20'}, {'id': '3'}]>
                                                Table "public.bible"
Column  |  Type   | Collation | Nullable |                 Default                 | Storage  | Stats target | Description 
---------+---------+-----------+----------+-----------------------------------------+----------+--------------+-------------
id      | integer |           |          | nextval('bible_id_seq'::regclass) | plain    |              | 
day     | text    |           |          |                                         | extended |              | 
book    | text    |           |          |                                         | extended |              | 
chapter | text    |           |          |                                         | extended |              | 
verse   | text    |           |          |                                         | extended |              | 
text    | text    |           |          |                                         | extended |              | 
link    | text    |           |          |                                         | extended |              | 
Indexes:
    "lesson_unique_id" UNIQUE CONSTRAINT, btree (id)
Referenced by:
    TABLE "notes_note" CONSTRAINT "notes_note_verse_id_5586a4bf_fk" FOREIGN KEY (verse_id) REFERENCES days_lesson(id) DEFERRABLE INITIALLY DEFERRED
Access method: heap