存储过程来更新mysql中每个重复记录的自动增量值

时间:2019-06-26 10:27:53

标签: mysql stored-procedures

我正在使用Mysql数据库创建新表。

我想做的是为每个重复项增加一个数字 值在另一列中的外观。例如,对于表:

两个表提供的order_placed列均应为空

DELIMITER $$
DROP procedure IF EXISTS `recordsequence` $$
CREATE procedure `recordsequence` () 
BEGIN

declare companyname varchar(250);
declare recordcount integer default 0;
DECLARE vfinished INTEGER DEFAULT 0;
declare duplcount integer default 0;
declare icount int default 0;
DEClARE records_cursor CURSOR FOR 
            select c.company_name,count(c.company_name) from contact c, req r where c.contact_id=r.contact_id and r.order_placed is null and r.status  group by c.company_name; 
  -- declare NOT FOUND handler
 DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET vfinished = 1;
    OPEN records_cursor;
    transfer_records: LOOP
    FETCH records_cursor INTO companyname,duplcount;## 

 - Heading

 ##
    IF vfinished = 1 THEN 
        LEAVE transfer_records;
    END IF;
 End$$
DELIMITER ;

存储重复的公司名称及其计数的程序。

Eg: req table                                          contact table

|req_id|order_placed| contact_id|seq_records| $ |company_name|order_placed| 
   1022      null          1000           1            Asus         null
   1025      null          1002           2            Asus         null
   1027      null          1003           3            Asus         null
   1028      null          1005           1            Vega         null
   1029      null          1006           2            Vega         null

预期结果

seq_records

像上面的示例一样,只要order_placed列均为空,我已经根据company_name列的值更新了{{1}}列。

0 个答案:

没有答案
相关问题