Invert id string into a table

1

I had to migrate a site that was made in pure PHP to Wordpress, searched the internet and managed to copy the data from the old site table to the Wordpress structure, that one thing happened, all the news was copied from behind to front, then when the news that are displaying first are the oldest, so by what I understood if I was able to reverse the order the IDs in the table I solve this, I searched the internet and found no example of how to do an UPDATE on a table with a sequential number.

In my bank has a column named "ID" the records comment from 1 and goes up to 114 I would like this ID to have the records reversed. EX: 114,113,112 .. until you reach number 1

    
asked by anonymous 04.05.2018 / 02:08

2 answers

4

I do not think reversing IDs is the best solution, but as you requested, it follows:

  • Back up your data if you have a problem with the following steps;

  • multiply this value by 2, and add 1. Note for use in query .

    Ex: If the highest ID is 213, the final value will be 427 (after all, 213 × 2 + 1 = 427).

  • Execute this query only once, replacing the example 427 with the value found in the previous step:

    UPDATE tabela SET id = 427 - id;
    

Done, you will have the IDs in reverse order. The only thing that can cause awkwardness is that they will not start from 1, but this should not be a problem for the intended purpose.

The reason we use a larger value is quite simple: You can not have two repeated IDs, so the calculation result above will always be greater than the ones in the table before UPDATE , ensuring that the execution does not contain a repeated index error.

If you really want to keep the numbering of 1 (unnecessary, but in any case ...) you can do this query after the previous one (again, once):

UPDATE tabela SET id = id - 213

This time subtracting the value of the highest ID obtained at the beginning of the steps.


I used different values to keep your focus on the steps and not just the final query . The values for the question are 229 and 114, respectively.

    
04.05.2018 / 03:41
2

I believe it's correct the way it is ... the display issue can be caused by another factor, configuration maybe. If you reverse the sequence, the next post would be code 115 and it would be just after the "hello world" which was the first one ... I recommend not to perform this procedure without first checking what it can be.

But if you look at just the reverse of numbering.

You first have to change all numbers to a range that has no conflicts. could do so:

update tabela set id = id + 1000;

When you do this, you make a select by numbering the rows in order of descending id and saving it in a temporary table:

with temp as 
(
 select t.*, row_number() over (order by t.id desc) as i from tabela t order by t.id desc 
)

Finally, you apply the new id in the record, as the number of the select row stored in the temporary table:

update tabela set id = (select x.i from temp x where x.id = tabela.id );
  

MySQL 8 was used as an example. The row_number () function is only available from this release.

For other versions, the following code can be used:

update tabela set id = id + 1000;

update tabela set id = (select x.i from (select t.*,  @rownum := @rownum + 1 AS i from tabela t, (SELECT @rownum := 0) r order by t.id desc ) x where x.id = tabela.id );

I put it in DBFiddle

  

Bacco's solution is clearly simpler and more functional for the case in question, but since I had already made the code to re-number it, I'll leave it here as well.

    
04.05.2018 / 03:44