MySQL列的值在上传后混合在一起。

huangapple go评论64阅读模式
英文:

mysql columns values are mixed after upload

问题

How do we update the table most effective way if 2 columns were uploaded incorrectly? After the upload I receive that some of the values from Name column moved to id column and vise versa.

the result of the upload:

Name id Age
didi 6666666 30
miki 7777777 27
reg 5647899 32
Gimi 1234567 23
1234566 Hani 40

How do I change in the last row between id and name value effectively?

Tried to update with value from name to Id - then id is erased and vise versa. Thanks

英文:

How do we update the table most effective way if 2 columns were uploaded incorrectly? After the upload I receive that some of the values from Name column moved to id column and vise versa.
the result of the upload :

Name id Age
didi 6666666 30
miki 7777777 27
reg 5647899 32
Gimi 1234567 23
1234566 Hani 40

How do I change in the last row between id and name value effectively?

Tried to update with value from name to Id - then id is erased and vise versa. Thanks

答案1

得分: 1

UPDATE test 
  SET id=(@id:=id), id=name, name=@id
  WHERE name NOT REGEXP '[^\\d]';
SELECT * FROM test;
Name id Age
didi 6666666 30
miki 7777777 27
reg 5647899 32
Gimi 1234567 23
Hani 1234566 40

fiddle

英文:
CREATE TABLE test (Name VARCHAR(64),	id VARCHAR(64),	Age INT);
INSERT INTO test VALUES
('didi',	'6666666',	30),
('miki',	'7777777',	27),
('reg',		'5647899',	32),
('Gimi',	'1234567',	23),
('1234566',	'Hani',		40);
SELECT * FROM test;
Name id Age
didi 6666666 30
miki 7777777 27
reg 5647899 32
Gimi 1234567 23
1234566 Hani 40
UPDATE test 
  SET id=(@id:=id), id=name, name=@id
  WHERE name NOT REGEXP '[^\\d]';
SELECT * FROM test;
Name id Age
didi 6666666 30
miki 7777777 27
reg 5647899 32
Gimi 1234567 23
Hani 1234566 40

fiddle

huangapple
  • 本文由 发表于 2023年6月8日 17:58:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定