MySQL update a field with an incrementing variable

2019-01-17 22:11发布

I have a table named 'textile_events' in one of my databases.

mysql> describe textile_events;

+-------------+--------------+-----+---------+----------------+
| Field       | Type         | Key | Default | Extra          |
+-------------+--------------+-----+---------+----------------+
| id          | int(11)      | PRI | NULL    | auto_increment |
| token       | varchar(20)  |     | NULL    |                |
| reg_time    | datetime     |     | NULL    |                |
| eid         | varchar(20)  |     | NULL    |                |
| fname       | varchar(20)  |     | NULL    |                |
| lname       | varchar(20)  |     | NULL    |                |
| paid        | varchar(10)  |     | NULL    |                |
| seq_no      | int(11)      |     | NULL    |                |
+-------------+--------------+-----+---------+----------------+
8 rows in set (0.00 sec)

mysql> select count(*) from textile_events;

+----------+
| count(*) |
+----------+
|     9325 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from textile_events where eid = 'headsup' ;

+----------+
| count(*) |
+----------+
|     2553 |
+----------+
1 row in set (0.01 sec)

'seq_no' field was introduced to the above table yesterday.

Now, I need to assign an incrementing number to 'seq_no' field for all 'headsup' events where paid field is equal to 'paid'.

In other way, I am looking for something like this,

$i = 250
while( true ):
    $i++
    UPDATE textile_events SET 'seq_no' = $i 
        WHERE eid = 'headsup' AND paid = 'paid'
endwhile;

How do I assign an incrementing number to a newly introduced field only to recods that satify a given condition?

What are the available options and what is the most efficient way to accomplish this?

标签: mysql mysqli
3条回答
手持菜刀,她持情操
2楼-- · 2019-01-17 22:20

Are you looking for something like this?

UPDATE textile_events e,
       (SELECT @n := 249) m
   SET e.seq_no = @n := @n + 1 
    WHERE e.eid = 'headsup' AND e.paid = 'paid'

SQLFiddle

查看更多
成全新的幸福
3楼-- · 2019-01-17 22:28

simple query would be, just set a variable to some number you want. then update the column you need by incrementing 1 from that number.

Ex:: Query to update all the rows where a column is null. it'll update each row id by incrementing 1

    SET @a  = 50000835 ;  
    UPDATE `civicrm_contact` SET external_identifier = @a:=@a+1 
    WHERE external_identifier IS NULL;

hope this helps some one. :)

查看更多
Melony?
4楼-- · 2019-01-17 22:38

May Be this will help you...

DELIMITER $$
    DROP PROCEDURE IF EXISTS manual_increment_count$$
    CREATE PROCEDURE manual_increment_count()
    BEGIN
     DECLARE count INT DEFAULT 0;
     SELECT COUNT(*) INTO count FROM textile_events 
     WHILE count > 0 
       SET count = count + 1;
       update textile_events 
            set seq_no = count  where eid = 'headsup' AND paid = 'paid'; 
     END WHILE;
    END$$
    DELIMITER ;
查看更多
登录 后发表回答