简体   繁体   English

MYSQL:如何通过将字符串值与整个表进行比较来替换字符串值的多个部分

[英]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;我有 table_1 和 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'; table_1 中的列是一个字符串,我们称之为 s1,多个值用逗号分隔,例如:'tmp,a1,a2,a6,a7'; the numbers after the 'a' are unique; 'a' 后面的数字是唯一的; in table_2 there are two columns: c1, c2;在 table_2 中有两列:c1、c2; c1 contains all the numbers after the 'a' in s1; c1 包含 s1 中 'a' 之后的所有数字; c2 cointains the numbers that need to replace the ones in s1, if they are null they dont need to be replaced. c2包含需要替换s1中的数字,如果它们是null则不需要替换。 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当'a'之后的所有数字等于c1中的数字并且c2不是null时,我需要用c2中的数字修改它们

How can i do this in mySQL我怎么能在 mySQL 中做到这一点

i tought of using the replace() function, but i dont know if i can insert a query inside of it我坚持使用 replace() function,但我不知道我是否可以在其中插入查询

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.我的一个朋友帮我解决了这个问题:它的作用是取值,把它放在一个字符串中,然后 substring 一块一块地,每次将它与表格进行比较,然后将其重新压缩为另一个值。

`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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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