希望有人可以帮我处理mysql查询
这就是我所拥有的:
我使用包含以下数据的“networkname”列表:
“VLAN-338-Network1-A,VLAN-364-Network2-A,VLAN-988-Network3-A,VLAN-1051-Network4-A”
我需要一个MySQL查询,它将更新该列,只有vlan编号按升序排列,删除其他所有内容。即。
“338, 364, 988, 1051”
谢谢, 大卫
答案 0 :(得分:0)
在此脚本中,我创建了一个循环networkname值的过程,并将数字解析为单独的表,然后使用group_concat
函数更新YourTable。这假设您的网络名称值遵循' VLAN-XXX'你的例子中的模式' XXX'是要提取的3-4位数字。这也假设每条记录都有一个唯一的ID。
CREATE PROCEDURE networkname_parser()
BEGIN
-- load test data
drop table if exists YourTable;
create table YourTable
(
ID int not null auto_increment,
networkname nvarchar(100),
primary key (ID)
);
insert into YourTable(networkname) values
('VLAN-338-Network1-A,VLAN-364-Network2-A,VLAN-988-Network3-A,VLAN-1051-Network4-A'),
('VLAN-231-Network1-A,VLAN-4567-Network2-A'),
('VLAN-9876-Network1-A,VLAN-321-Network2-A,VLAN-1678-Network3-A');
-- add commas to the end of networkname for parsing
update YourTable set networkname = concat(networkname,',');
-- parse networkname into related table
drop table if exists ParseYourString;
create table ParseYourString(ID int,NetworkNumbers int);
while (select count(*) from YourTable where networkname like 'VLAN-%') > 0
do
insert into ParseYourString
select ID,replace(substr(networkname,6,4),'-','')
from YourTable
where networkname like 'VLAN-%';
update YourTable
set networkname = right(networkname,char_length(networkname)-instr(networkname,','))
where networkname like 'VLAN-%';
end while;
-- update YourTable.networkname with NetworkNumbers
update YourTable t
inner join (select ID,group_concat(networknumbers order by networknumbers asc) as networknumbers
from ParseYourString
group by ID) n
on n.ID = t.ID
set t.networkname = n.networknumbers;
END//
调用程序并选择结果:
call networkname_parser();
select * from YourTable;