Ciao a tutti
ho una tabella con 200 campi, vorrei sostituire il valore null presente in diversi campi
con il pipe |
come posso fare?
Grazie
Ciao a tutti
ho una tabella con 200 campi, vorrei sostituire il valore null presente in diversi campi
con il pipe |
come posso fare?
Grazie
hai ragione mysql.
grazie
In pratica interrogando l'information_schema (adatta nome_tabella e nome_database al tuo contesto), generi la stringa di update in modo dinamico e poi esegui la query tramite prepared statement.codice:set @str = (select concat('update ',table_name, ' set \r\n', group_concat(concat(column_name,' = case when ',column_name," is null then '|' else ",column_name,' end\r\n'))) from information_schema.columns where table_name = 'nome_tabella' and table_schema = 'nome_database'); prepare stmt from @str; execute stmt; deallocate prepare stmt;
Nei column_name come vanno impostati i valori?
group_concat(concat(column_name ,' = case when ',column_name ," is null then '|' else ",column_name ,' end\r\n')))
Le uniche cose che devi modificare come ti ho già scritto sono il nome della tabella e del db. I nomi dei campi vengono ricavati dinamicamente.
Ti basta fare un
select @str;
per vedere la query che viene generata prima della sua esecuzione.
Ciao mi compare questo errore
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1957 = case when 1957 is null then '|' else 1957 end
,1986 = case when 1986 is ' at line 3"
Non potevi scegliere nomi peggiori per i campi. Devi fare l'escape. Prova così.
codice:set @str = (select concat('update ',table_name, ' set \r\n', group_concat(concat('`',column_name,'` = case when `',column_name,"` is null then '|' else `",column_name,'` end\r\n'))) from information_schema.columns where table_name = 'nome_tabella' and table_schema = 'nome_db');
Mi da query eseguita con sucesso ma non mi modifica il valore null.
Grazie
Accertati che i campi contengano effettivamente NULL e non la stringa null.
select * from tabella where `1986` is null
![]()