简体   繁体   English

SQL-UPDATE TABLE和ORDER BY?

[英]SQL - UPDATE TABLE and ORDER BY?

So I have an unordered table movement that has columns timestamp , x ,and y . 所以我有一个无序的表movement ,它具有列timestampxy I want to order by timestamp , and change and save the table to have the all rows ordered by timestamp. 我想按timestamp排序,然后更改并保存表以使所有行按时间戳排序。

I wanted to use UPDATE TABLE but I'm unsure on how the query would look... for example, I have this: 我想使用UPDATE TABLE但是不确定查询的外观...例如,我有以下内容:

UPDATE movement 
SET ????? 
ORDER BY timestamp; 

I don't want to set anything, I just want to change the order of the table. 我不想设置任何东西,我只想更改表的顺序。 I need to do this for another query that I'm going to write, but the fact is that this table is very large and I need to break up things in steps so that the performance isn't terrible later. 我需要对要编写的另一个查询执行此操作,但事实是该表非常大,我需要分步进行操作,以免稍后性能不佳。 So how would I do this? 那我该怎么做呢? I found this SO post that addressed a similar question, but I was wondering if there was another way to do it rather than use another table, because as I said, this table is very large(millions of rows) and recopying the table would take a long time. 我发现 SO帖子解决了类似的问题,但我想知道是否还有另一种方法可以代替使用另一个表,因为正如我所说,该表非常大(数百万行),因此重新复制该表将花费很多时间很长时间。

Tables don't inherently have an order; 表格本质上并没有顺序。 you don't have to update them into any particular order. 您不必将它们更新为任何特定顺序。

What you do is choose the order of what you SELECT from the table. 您要做的是从表中选择选择的顺序。 Then it can be in any order you choose! 然后可以按照您选择的任何顺序!

Example: 例:

SELECT * FROM movement  
ORDER BY timestamp; 

But then somewhere else maybe you want to: 但是在其他地方,您可能想要:

SELECT * FROM movement  
ORDER BY timestamp DESCENDING; 

You can't use ORDER BY in UPDATE statement. 您不能在UPDATE语句中使用ORDER BY ORDER BY should be used with SELECT statement only. ORDER BY应该仅与SELECT语句一起使用。 Again, there is no need of having the records stored in particular order cause you can just display the records in particular order with a SELECT statement like 同样,无需按特定顺序存储记录,因为您可以使用SELECT statement按特定顺序显示记录,例如

select * from movement 
order by timestamp

Relational database tables represent unordered sets. 关系数据库表表示无序集。 There is no syntax for sorting a table, simply because there is no such concept as the order of rows in a table. 没有用于对表进行排序的语法,仅仅是因为没有诸如表中的行顺序之类的概念。 When you issue a query without an explicit order by clause, the database may return the rows to you in any order it may see fit, which might be influenced by the order they were inserted and written to disk, their presence in some memory cache, indexes, or a host of other implementation details. 当您在没有明确的order by子句的情况下发出查询时,数据库可能会以合适的任何顺序将行返回给您,这可能受到行插入和写入磁盘的顺序,它们在某些内存缓存中的存在,索引或其他许多实现细节。

If you want to query the table's rows sorted by their timestamp, just explicitly state it in the order by clause: 如果要查询按其时间戳排序的表行,只需在order by子句中明确声明它即可:

SELECT   *
FROM     `movement`
ORDER BY `timestamp`

It is actually possible. 实际上是可能的。 This is in MySQL format... Update is for editing already existing information. 这是MySQL格式...更新用于编辑已经存在的信息。 If you want to make more direct changes, use ALTER or MODIFY according to syntax. 如果要进行更直接的更改,请根据语法使用ALTER或MODIFY。

ALTER TABLE movement 
ORDER BY timestamp;

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

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