MySQL Update null column values incrementally in time order

Peter Bushnell

There is a table with hundreds of entries and six columns. One of those columns, the 'transaction' column, has been filling up with null values, we now need that column to have unique values, existing entries are unique.

There is a 'time' datetime column, ideally the 'transaction' column would be updated incrementally with integers starting from 1 in 'time' order.

This seems simple but after several times dropping and restoring a live database it now seems less simple and a little help would be greatly appreciated.

SQL Fiddle - Sample of live database

Final solution many thanks to Juan Carlos Oropeza for the solution and being patient with me.

UPDATE `my_table` t1 
INNER JOIN (SELECT t.*, 
               @rownum := @rownum + 1 AS `rank`
        FROM `my_table` t, 
             (SELECT @rownum := 0) r
        ORDER BY time
       ) t2
ON t1.`id` = t2.`id`
SET t1.`transaction` = t2.`rank` where t1.`transaction` is null
Juan Carlos Oropeza

SQL Fiddle Demo

UPDATE Table1 t1 
INNER JOIN (SELECT t.*, 
                   @rownum := @rownum + 1 AS `rank`
            FROM Table1 t, 
                 (SELECT @rownum := 0) r
            // ORDER BY time   use your datetime field
           ) t2
    ON t1.`ID` = t2.`ID`    // You need a PK field
SET t1.`value` = t2.`rank`  // Update your sequence field

Schema

CREATE TABLE Table1
    (`ID` int, `value` int)
;

INSERT INTO Table1
    (`ID`, `value`)
VALUES
    (10, NULL),
    (20, NULL),
    (30, NULL)
;

OUTPUT

| ID | value |
|----|-------|
| 10 |     1 |
| 20 |     2 |
| 30 |     3 |

EDIT:

or just create a new ID field to be PK

DEMO

alter table `wp_user_txs` add column `id` int(10) unsigned primary KEY AUTO_INCREMENT;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Order by NULL values with mysql

From Dev

mysql : order by then select incrementally (Pagination)

From Dev

update column values which are null

From Dev

update time to date-time column in MySQL

From Java

MySQL Update column a if not null and update column b if not null

From Dev

Oracle - Order by column with NULL values in last position

From Dev

Oracle - Order by column with NULL values in last position

From Dev

MySQL Update Sort Order Column Using Counter

From Dev

MySQL update all rows to add time to timestamp gives: "Column 'start' cannot be null"

From Dev

mysql order by field with NULL values last

From Dev

Update column with subquery, filter out null values

From Dev

How to update timestamp values in column by adding a specific time (seconds) to the existing timestamp using mysql?

From Dev

mysql update based on time stamp in another column

From Dev

Time type column field data update in mysql

From Dev

how to update column that has (NULL) value to 'A' in mysql

From Dev

Mysql - update table column from another column based on order

From Dev

Update All Values in MySQL Column to Follow Pattern

From Dev

MySql update with different values a column for duplicate columns

From Dev

Mysql: Multiple column primary key with null values

From Dev

Sort values in two column and insert order position into another column in mysql

From Dev

In which order Oracle SQL update statement changes column values?

From Dev

SQL: Count occurrences of column values within a time range and order by count

From Dev

How to order a query by column if some of its values are null?

From Dev

Mysql Update Query , update column values in one query

From Dev

Mysql: Update json column with values from different table/column

From Dev

MySQL column set to NOT NULL but still allowing NULL values

From Dev

Update list of time values from single column in SQL Server?

From Dev

Find date and time values in text and then update another column

Related Related

  1. 1

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

  2. 2

    Order by NULL values with mysql

  3. 3

    mysql : order by then select incrementally (Pagination)

  4. 4

    update column values which are null

  5. 5

    update time to date-time column in MySQL

  6. 6

    MySQL Update column a if not null and update column b if not null

  7. 7

    Oracle - Order by column with NULL values in last position

  8. 8

    Oracle - Order by column with NULL values in last position

  9. 9

    MySQL Update Sort Order Column Using Counter

  10. 10

    MySQL update all rows to add time to timestamp gives: "Column 'start' cannot be null"

  11. 11

    mysql order by field with NULL values last

  12. 12

    Update column with subquery, filter out null values

  13. 13

    How to update timestamp values in column by adding a specific time (seconds) to the existing timestamp using mysql?

  14. 14

    mysql update based on time stamp in another column

  15. 15

    Time type column field data update in mysql

  16. 16

    how to update column that has (NULL) value to 'A' in mysql

  17. 17

    Mysql - update table column from another column based on order

  18. 18

    Update All Values in MySQL Column to Follow Pattern

  19. 19

    MySql update with different values a column for duplicate columns

  20. 20

    Mysql: Multiple column primary key with null values

  21. 21

    Sort values in two column and insert order position into another column in mysql

  22. 22

    In which order Oracle SQL update statement changes column values?

  23. 23

    SQL: Count occurrences of column values within a time range and order by count

  24. 24

    How to order a query by column if some of its values are null?

  25. 25

    Mysql Update Query , update column values in one query

  26. 26

    Mysql: Update json column with values from different table/column

  27. 27

    MySQL column set to NOT NULL but still allowing NULL values

  28. 28

    Update list of time values from single column in SQL Server?

  29. 29

    Find date and time values in text and then update another column

HotTag

Archive