简体   繁体   English

Postgres 按更改排序

[英]Postgres order by change

I am trying to change the order by ID in my Postgres questions database.我试图在我的 Postgres 问题数据库中按 ID 更改顺序。

It is ordered randomly for some reason, but I want it to be ordered by ID ASC.由于某种原因,它是随机订购的,但我希望它由 ID ASC 订购。

I am very new to postgres so I don't know what I am doing..,我对 postgres 很陌生,所以我不知道我在做什么..,

I just want the result of this query:我只想要这个查询的结果:

SELECT * 
FROM public.questions
ORDER BY id ASC

but I don't know how to update my database with its order that shows the query output.但我不知道如何使用显示查询输出的顺序更新我的数据库。 It doesn't let me save it, I know im doing something wrong.它不允许我保存它,我知道我做错了什么。 How do I achieve it?我如何实现它?

This is the current order, id goes like 1,2 then 4这是当前的顺序,id 是 1,2 然后是 4

这是当前的顺序,ID 是 1,2 然后是 4

I want it to look like this in perfect order by id 1,2,3,4我希望它按照 id 1,2,3,4 的完美顺序看起来像这样

我希望它按照 ID 1、2、3、4 的完美顺序看起来像这样

The reason why I want do this because I want my questions in my container to appear in order by ID, here my codes, any thoughts how i do achieve that?我之所以要这样做,是因为我希望我的容器中的问题按 ID 顺序出现,这里是我的代码,有什么想法我是如何实现的?

class QuestionContainer extends React.Component {
  constructor() {
    super();
    this.state = {
      questions: [],
    };
    this.SubmitQuestion = this.SubmitQuestion.bind(this);
  }

  componentDidMount() {
    axios.get(`${env.url}/api/v1/en/questions?limit=100`).then((res) => {
      const qes = res.data.questions.map((a) => ({
        questionId: a.question_no,
        question: a.question,
      }));
      this.setState({ questions: qes });
    }, (err) => {
      console.log('Question fetch error ', err);
      this.setState({
        questions: [{
          questionId: 1,
          question: 'All',
        }],
      });
    });
  }

You always have to add ORDER BY if you want the result to be in a certain order.如果您希望结果按特定顺序排列,则始终必须添加ORDER BY

Tables have a physical order, but in PostgreSQL that changes whenever you modify data, and even if you don't modify data you are not guaranteed to get the results in a certain order.表具有物理顺序,但在 PostgreSQL 中,只要您修改数据,它就会发生变化,即使您不修改数据,也不能保证按特定顺序获得结果。

There's a few things:有几件事:

  1. when tuples are retrieved it is not guaranteed that they will be sorted in a consistent manner.当检索元组时,不能保证它们会以一致的方式排序。 They are compiled into a set for speed, which may be a random order.为了速度,它们被编译成一个集合,这可能是一个随机顺序。 The way to enforce a sort order is to use the ORDER BY clause.强制排序顺序的方法是使用ORDER BY子句。 Without it, there is no guarantee没有它,就没有保证
  2. I'd advise against using the id (primary key) as the ordered column.我建议不要使用id (主键)作为有序列。 If order is important, I'd either add an order column to the table, or what I prefer is to have a timestamp field.如果订单很重要,我会在表中添加一个order列,或者我更喜欢有一个时间戳字段。 In your case, though, it looks like question_no has a semantic order, so I might have used that over id .但是,在您的情况下, question_no似乎具有语义顺序,因此我可能已经在id使用了它。
  3. your example looks like it's using JavaScript React;您的示例看起来像是使用 JavaScript React; you can get the order from the database, or you can update the order in your presentation logic.您可以从数据库中获取订单,也可以在演示逻辑中更新订单。 Below I've used .sort((a,b)=>a.questionId - b.questionId) , which is a quick example:下面我使用了.sort((a,b)=>a.questionId - b.questionId) ,这是一个简单的例子:

     const qes = res.data.questions.map((a) => ({ questionId: a.question_no, question: a.question, })).sort((a,b)=>a.questionId - b.questionId);
  4. If you wanted to try and retrieve it without the sort, I think you can pass an argument to the axios API.如果您想尝试在没有排序的情况下检索它,我认为您可以将参数传递给 axios API。 So this line:所以这一行:

     axios.get(`${env.url}/api/v1/en/questions?limit=100`)

    Would be changed to:将改为:

     axios.get(`${env.url}/api/v1/en/questions?orderBy=\\"question_no\\"&limit=100`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM