Incrementally update date field column based on order of a second column

EyePeaSea

MSQL DB for WordPress site.

I would like to update a date column for each row in a table, setting the date/time to an incrementing value (e.g. increment value by 1 day). The order that I do the updates should be based on the textual sort of another column in the row. The actual date value used isn't relevant (it could start at 2001-01-01 00:00:00 and increment in seconds, minutes, hours or days).

Example of two columns in the table:

field_date         , title_text
2014-08-20 09:00:27, AAA-Entry
2014-08-24 10:00:00, ZZZ-Entry
2014-08-27 10:15:00, MMM-Entry

So, the row with the first 'title_text' (alphabetical sorting) should get the newest date. The next row based on title_text should get a slightly older date and so on until the last row (based on title_text) has the oldest date. So the data shown above would end up looking something like:

field_date         , title_text
2000-01-03 00:00:00, AAA-Entry
2000-01-01 00:00:00, ZZZ-Entry
2000-01-02 00:00:00, MMM-Entry

After the update then the command: select * from tbl_name order by field_date

would show output that would also be in alphabetical order of the title_text field. This would be equivalent to running: select * from tbl_name order by title_text

I'm looking at: [Incrementing datetime field with an update statement for SQL2005 and trying to get this to work on mySQL as I think this would work (I just change the 'order by' statement to 'order by title_text'). But I'm having problems with converting this to mySQL.

I'd appreciate any suggestions on different approaches or with getting the above solution to work in mySQL.

Thanks

Ian

Note: The reason behind this is that Wordpress populates list boxes/search results, based on the posting date, but I want it to be ordered based on the title text. There may be a 'Wordpress' answer (or plugin) to do this (and if you know, please feel free to tell me ;-) ), but from a learning viewpoint, I'd also like to understand how this was possible using mySQL.

Gordon Linoff

In MySQL, you can do updates with sorts. The easiest way is to define a variable first and then do the update:

set @rn = -1;

update t
    set field_date = date('2000-01-01') + interval (@rn := @rn + 1) day
    order by title_text desc;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL Update null column values incrementally in time order

From Dev

sort based on the second field and then re-order the lines having the same first column but still keep the order on second field for each group

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

Adding the values of second column based on date and time of first column

From Dev

Adding the values of second column based on date and time of first column

From Dev

Obtaining Result Based on Date Field of Another Column

From Dev

Mysql - update table column from another column based on order

From Dev

Update column value in XTS object based on date

From Dev

Update column with previous record based on date

From Dev

MySQL Update Value of column Based on DATE

From Dev

Stored procedure to update column based on Min Date

From Dev

Update table column that is used for ordering according to alphabetical order on a second table

From Dev

How to update a column in one table based on values from a second table?

From Dev

Fill in missing date values and populate second column based on previous row

From Dev

Update existing records based on the order from a different column

From Dev

How to update mysql date column field with the next working day.?

From Dev

Adding column based on matching of second column

From Dev

Pandas: Incrementally count occurrences in a column

From Dev

Need help in order column by date

From Dev

MYSQL order by date of a datetime column

From Dev

R create a column based on duplicate values of one column, and a second column

From Dev

update order column in a table but maintain the order

From Dev

Update a column based on other column with postgresql

From Dev

Update a column in a table based on column in another table

From Dev

Update a column based on the value of other column

From Dev

select column values based on other column date

From Dev

select column values based on other column date

From Dev

Python calculate a new column based on Date column

From Dev

Need to update a column based on ORDER BY clause : Error 1093 Can't specify target table for update in FROM clause

Related Related

  1. 1

    MySQL Update null column values incrementally in time order

  2. 2

    sort based on the second field and then re-order the lines having the same first column but still keep the order on second field for each group

  3. 3

    UPDATE sql column with value from another column based on a date column

  4. 4

    Adding the values of second column based on date and time of first column

  5. 5

    Adding the values of second column based on date and time of first column

  6. 6

    Obtaining Result Based on Date Field of Another Column

  7. 7

    Mysql - update table column from another column based on order

  8. 8

    Update column value in XTS object based on date

  9. 9

    Update column with previous record based on date

  10. 10

    MySQL Update Value of column Based on DATE

  11. 11

    Stored procedure to update column based on Min Date

  12. 12

    Update table column that is used for ordering according to alphabetical order on a second table

  13. 13

    How to update a column in one table based on values from a second table?

  14. 14

    Fill in missing date values and populate second column based on previous row

  15. 15

    Update existing records based on the order from a different column

  16. 16

    How to update mysql date column field with the next working day.?

  17. 17

    Adding column based on matching of second column

  18. 18

    Pandas: Incrementally count occurrences in a column

  19. 19

    Need help in order column by date

  20. 20

    MYSQL order by date of a datetime column

  21. 21

    R create a column based on duplicate values of one column, and a second column

  22. 22

    update order column in a table but maintain the order

  23. 23

    Update a column based on other column with postgresql

  24. 24

    Update a column in a table based on column in another table

  25. 25

    Update a column based on the value of other column

  26. 26

    select column values based on other column date

  27. 27

    select column values based on other column date

  28. 28

    Python calculate a new column based on Date column

  29. 29

    Need to update a column based on ORDER BY clause : Error 1093 Can't specify target table for update in FROM clause

HotTag

Archive