简体   繁体   中英

MYSQL: how can i replace multiple parts of a string value by comparing it to an entire table

I have table_1 and table_2; the column in table_1 is a string, lets call it s1, with multiple values separated by comma, EXAMPLE: 'tmp,a1,a2,a6,a7'; the numbers after the 'a' are unique; in table_2 there are two columns: c1, c2; c1 contains all the numbers after the 'a' in s1; c2 cointains the numbers that need to replace the ones in s1, if they are null they dont need to be replaced. I need to modify all the numbers after the 'a' with the numbers in c2 when they are equal to the numbers in c1 and when c2 is not null

How can i do this in mySQL

i tought of using the replace() function, but i dont know if i can insert a query inside of it

EDIT: i know this violate first rule of integrity, but i have no choice since i was told to not modify existing table

A friend of mine helped me solve this problem: so what it does it take the value, put it in a string, then substring it piece by piece and each time comparing it to the table and then it recompress it into another value.

`CREATE DEFINER=`root`@`%` PROCEDURE `sp_ik_rinomina_campi_extra`(in nome_tabella varchar(200), in nome_colonna varchar(200))
BEGIN
    
  /* dichiarazione variabili */

  DECLARE finished INTEGER default 0;
  declare valore_colonna varchar(2000);
  declare OLD_valore_colonna varchar(2000);
  declare NEW_valore_colonna varchar(2000);
  declare app_valore varchar(100);
  declare cnt integer;
  declare old_id integer;
  declare new_id integer;
  declare cur_cri cursor for

    SELECT col1 FROM app_cri;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
  /* fine dichiarazione variabili */     
  select '1';
  /* controllo se esiste la tabella temporanea e in caso la cancello */

  set @ret = 0;

  SELECT count(1) INTO @ret FROM information_schema.TABLES

         WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'app_cri';
  if @ret = 1 then
    drop table app_cri;
  end if;
  
  set @ret = 0;

  SELECT count(1) INTO @ret FROM information_schema.TABLES
         WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ext_result_mappatura';

  if @ret = 1 then
    drop table ext_result_mappatura;
  end if;

    create table if not exists app_cri
    (
    col1 varchar(4000)
    );

  create table ext_result_mappatura

  (
    old_campo varchar(4000) ,
    new_campo varchar(4000)
   );

  TRUNCATE TABLE app_cri;

 

  set @sql = 'insert into app_cri select ';    
  set @sql = concat(@sql,nome_colonna);
  set @sql = concat(@sql,' from ');
  set @sql = concat(@sql,nome_tabella);

      

  PREPARE dynamic_statement FROM @sql;
  EXECUTE dynamic_statement;
  DEALLOCATE PREPARE dynamic_statement;     

 

  open cur_cri;        


  getcur: loop
    fetch next from cur_cri into old_valore_colonna;
    if finished = 1 then
      leave getcur;
    end if; 

    

    set valore_colonna = concat(old_valore_colonna,','); /*aggiungo una , in fondo */
    set new_valore_colonna = ''; /* pulisco il campo new */

   

    while valore_colonna <> '' do

      if new_valore_colonna <> '' then
          set new_valore_colonna = concat(new_valore_colonna,',');
      end if;

      set app_valore = (SELECT SUBSTRING_INDEX(valore_colonna, ',', 1));


      if substring(app_valore,1,5) = 'Extra' then
        set NEW_valore_colonna = concat(new_valore_colonna,'Extra');
        set old_id = substring(app_valore,6);
        set new_id = 0;

        set new_id = (select nuovivalori from TempTable6 where vecchivalori = old_id);

       

        if new_id > 0 then

          set new_valore_colonna = concat(new_valore_colonna,new_id);
        else

          set new_valore_colonna = concat(new_valore_colonna,old_id);
        end if;

      else

        set new_valore_colonna = concat(new_valore_colonna,app_valore);
      end if;
      
      set valore_colonna = substring(valore_colonna,LOCATE(',', valore_colonna)+1);
    end while; 

    insert into ext_result_mappatura values (old_valore_colonna,new_valore_colonna);

  end loop getcur;
  
  close cur_cri;
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM