简体   繁体   中英

Sort MySQL table by timestamp

I got a data acquisition board running, and I generate a php code to extract the data every 15 min and then insert it in a table.

This works fine. However, if for some reason the connection is lost or some value is missing, I want to generate a recover function, so I figure out how to ask the board for some values, but I need to figure out how to post the recovered data and then sort the whole table order by timestamp.

For that I want to use php, and I place this code

$result = mysql_query("SELECT * FROM `table` ORDER BY `date` DESC;" );

But this doesn't cause any effect. I need to sort the table not the result.

Is there any easy way to do that?

EDIT :

The data is extracted with a timestamp, now my table has the following structure:

id  |  TimeStamp  |  Temperature
  • my id is INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  • timestamp
  • temperature is varchar(15)

The php function gets the last value of the adquicition board and insert it on the last id. I just place the time stamp to make a historial of the data. But I want the table to be sort by this field, in case of a recovery. To make a recovery the call function to the board is way different to the continuous mode.That's why I use sequencial insert and I was wandering if after the lost data recover is done I could sort the whole table using the timestamp.

From my reading of the question this pertains to a wish that data in the table will be inserted according to TimeStamp rather than being inserted sequentially.

Perhaps if the author could clarify how this need arises the SO community may be able to help more.

I know that this is not an answer you want to hear, but - unfortunately - it's true: there is no such thing as "order of table" - tables are a SETS of rows, not SEQUENCES of rows. This is why you can only order results, not the table itself (the operation would be meaningless).

What you need to do is to rethink your design, spend some time with a good book about databases and get rid of some false assumptions.

If what you want to achieve is "making sure that primary keys are ordered according to the timestamps", well - you should NOT want that, there are ways, but they are all insane. The simplest (probably) would be to dump all the rows to some other table using select into , then delete them from your original table and insert back again. Anyway: don't try this. You DON'T need your table ordered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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