我的一个数据库中有一个名为“ textile_events”的表。
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”字段被引入到上表中。
现在,对于所有付费字段等于“已付费”的“平头”事件,我需要为“ seq_no”字段分配一个递增编号。
换句话说,我正在寻找这样的东西,
$i = 250 while( true ): $i++ UPDATE textile_events SET 'seq_no' = $i WHERE eid = 'headsup' AND paid = 'paid' endwhile;
如何仅将满足特定条件的Recod分配给新引入的字段一个递增编号?
有哪些可用的选项,最有效的方法是什么?
您是否正在寻找这样的东西?
UPDATE textile_events e, (SELECT @n := 249) m SET e.seq_no = @n := @n + 1 WHERE e.eid = 'headsup' AND e.paid = 'paid'
SQLFiddle