php - Wildcard column name in UPDATE query -
assuming have sql table called techsched
setup below 800,900 etc. etc. went on till 2300, each representing 1 hour block of time. 6 character alphanumeric character (tpi232) represents ticket number assigned tech , ticket number duplicated across each block of time tech scheduled for. need run sql query can find ticket number regardless of located in table , set value of row/column found null or blank
tech date 800 900 1000 1100 1200 john doe 05-01-15 dsa123 dsa123 dsa123 mike doe 05-01-15 fgg342 fgg342 bill doe 05-01-15 steve doe 05-01-15 tpi232 tpi232 tpi232
i know below not , not work best way demonstrate i'm trying do:
update techsched
set wildcard_column_name='' wildcard_column_name='fgg342'
i don't know record number may occur @ in table, how can accomplished?
create stored procedure , tweak suit case:
delimiter $$ drop procedure if exists clear_values$$ create procedure clear_values(subject char(10)) begin declare finished int default 0; declare colname varchar(100); -- cursor declare cur_columns cursor select column_name information_schema.columns table_name = 'test' , data_type = 'char' , character_maximum_length = 10; -- data type , length matches field info -- in table -- handler when run out of records read declare continue handler not found set finished = 1; open cur_columns; reading: loop -- retrieve data until end of records fetch cur_columns colname; if finished = 1 leave reading; end if; -- create text update column's value set @statement = concat( 'update test ', 'set `', colname, '` = \'\' ', 'where `', colname, '` = \'', subject, '\'' ); -- create prepared statement text -- , execute prepare stmt @statement; execute stmt; deallocate prepare stmt; end loop reading; close cur_columns; end$$ delimiter ;
when chance, think normalizing table assuming small project:
create table techs ( id int auto_increment primary key, tech varchar(50) ); create table schedules ( id int auto_increment primary key, tech_id int not null, sched datetime not null, ticket char(6), constraint fk_schedules_techs_tech_id foreign key (tech_id) references techs (id), constraint uk_schedules_tech_id_sched unique (tech_id, sched) ); insert techs (tech) values ('joe'), ('matt'); insert schedules (tech_id, sched, ticket) values (1, '2015-05-01 08:00:00', ''), (1, '2015-05-01 09:00:00', ''), (1, '2015-05-01 10:00:00', 'dsa123'), (2, '2015-05-01 08:00:00', 'fgg324'), (2, '2015-05-01 09:00:00', 'fgg324'), (2, '2015-05-01 10:00:00', '');
http://sqlfiddle.com/#!9/19bc3/1
now when have clear out ticket ticket fgg324, can type:
update schedules set ticket = '' ticket = 'fgg324';
Comments
Post a Comment