开发者

Insert Data Into Column

开发者 https://www.devze.com 2023-03-05 12:30 出处:网络
Alright, I have a column named id which has 2000 \"ids\" in them, and I have a newly added field called \"name\".

Alright, I have a column named id which has 2000 "ids" in them, and I have a newly added field called "name".

I was wondering how would I go about inserting a list of names so they line up to their corresponding id?

id || name

1 || bob
2 || smith
3 || john
4 || louis
5 || kevin
6 || jacob
7 || tim

My apologies for not being to clear with the question. id and name are both in the same table 开发者_开发问答"cards". The cards were added in the past with other bits of data like descriptions, colors, etc so there are already quite a lot of id's that have been generated. I was just asked to add a name field to the table "cards" and insert data from a spread sheet into said name field.


If you've got the data in a spreadsheet, with 2 columns, id and name, you could do the following:

In the 3rd column of the spreadsheet, do

=CONCATENATE("UPDATE cards SET name = '",A1,"' WHERE id = ",A2,";")

then drag that formula down all rows. You can then just run the queries generated. For 2000 rows, it shouldn't take long.


You need a known relationship between id and name. So your name list must contain the id's before you use it to update your table. Since this must be the case, create a temporary table with id, name and use it to update the main table. e.g.

CREATE TEMPORARY TABLE tmptable(id INT(10) UNSIGNED PRIMARY KEY, 
                                firstname VARCHAR(20));
LOAD DATA INFILE '/path/to/mynames.txt' INTO tmptable(id, firstname);
UPDATE mytable m JOIN tmptable t ON (m.id=t.id) SET m.firstname=t.firstname;

Check out LOAD DATA INFILE syntax here.

0

精彩评论

暂无评论...
验证码 换一张
取 消