Php 更新查询中的通配符列名
假设我有一个名为Php 更新查询中的通配符列名,php,mysqli,sql-update,Php,Mysqli,Sql Update,假设我有一个名为techschedsetup的SQL表,如下所示,但800900等一直持续到2300,每个表都代表一个小时的时间段。六个字符的字母数字字符(TPI232)表示分配给tech的票号,票号在tech计划的每个时间段内重复。我需要运行一个SQL查询,该查询可以查找票证号,而不管它在表中的位置如何,并将找到它的行/列的值设置为NULL或空白 Tech date 800 900 1000 1100 1200 John
techsched
setup的SQL表,如下所示,但800900等一直持续到2300,每个表都代表一个小时的时间段。六个字符的字母数字字符(TPI232)表示分配给tech的票号,票号在tech计划的每个时间段内重复。我需要运行一个SQL查询,该查询可以查找票证号,而不管它在表中的位置如何,并将找到它的行/列的值设置为NULL或空白
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
技术日期800 900 1000 1100 1200
John Doe 05-01-15 DSA123 DSA123 DSA123
迈克·多伊05-01-15 FGG342 FGG342
法案Doe 05-01-15
Steve Doe 05-01-15 TPI232 TPI232 TPI232
我知道下面这句话行不通,也行不通,但这是证明我正在努力做的事情的最好方式:
更新techsched
SET wildcard\u column\u name='',其中wildcard\u column\u name='FGG342'
我不知道记录编号可能出现在表中的何处,因此如何才能做到这一点?我建议您重新设计表的结构。例如,列可以是“tech、date、hour、ticket”。创建这样的存储过程,并根据您的情况对其进行调整:
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 for
select column_name
from information_schema.columns
where table_name = 'test'
and data_type = 'char'
and character_maximum_length = 10;
-- data type and length matches the field info
-- in my table
-- handler for when we run out of records to read
declare continue handler for not found
set finished = 1;
open cur_columns;
reading: loop
-- retrieve data until end of records
fetch cur_columns into colname;
if finished = 1 then
leave reading;
end if;
-- create text that will update column's value
set @statement = concat(
'update test ',
'set `', colname, '` = \'\' ',
'where `', colname, '` = \'', subject, '\''
);
-- create a prepared statement from the text
-- and execute it
prepare stmt from @statement;
execute stmt;
deallocate prepare stmt;
end loop reading;
close cur_columns;
end$$
delimiter ;
如果有机会,可以考虑在某种程度上规范化表,假设这是一个小项目:
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 into techs (tech) values
('Joe'),
('Matt');
insert into 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', '');
现在,当您必须清除票证所在的票证FGG324时,您可以键入:
update schedules set ticket = '' where ticket = 'FGG324';
什么是
8009001000..
?列名或值它们都是列名感谢您的输入,我同意,但在这一点上,项目太大,当前的表设置太嵌入到其他代码中,无法对其进行不同的修改。我真的想把这个项目推迟一个月。谢谢你,我很抱歉推迟了我接受手术的时间,因为我已经离开了好几天。我以前没有使用过存储过程,所以我现在正在探索。希望你的手术顺利进行。存储过程是工作探索。一本或两本关于MySQL数据库开发的书有时会很方便