[英]How to add information to a specific column in a pre-existing database
I have a database named week1
which includes columns: age, address, voternum, voterhistory, status, gender, precinct, county, and zip5
. 我有一个数据库名为
week1
其中包括列: age, address, voternum, voterhistory, status, gender, precinct, county, and zip5
。
The information for all rows excluding voterhistory
is already in the table! 该表中已包含除
voterhistory
之外的所有行的信息!
I need to import the voterhistory
column separately from a text file named vhist into the table where voternum
of the week1 table is equal to voternum
of the vhist text file. 我需要导入
voterhistory
从vhist命名为表,其中一个文本文件中单独列voternum
的week1表等于voternum
的vhist文本文件。
Multiple lines from the text file can be put into the voterhistory
column and should be separated by a comma. 可以将文本文件中的多行内容放入“
voterhistory
列,并以逗号分隔。
For example: 例如:
Within week1 there is a person with voternum = 1234
. 在第1
voternum = 1234
有一个voternum = 1234
。
In the vhist text file there are 3 records where voternum = 1234
. 在vhist文本文件中,有3条记录,其中
voternum = 1234
。
One has a voterhistory = 2011
, one has a voterhistory = 2012
, and one has a voterhistory = 2013
. 一个人的
voterhistory = 2011
,一个人的voterhistory = 2012
,一个人的voterhistory = 2013
。
This means when importing the vhist text file those 3 records should import into the voterhistory
column of the week1 table like so: 2011, 2012, 2013
. 这意味着在导入vhist文本文件时,这3条记录应导入到
voterhistory
表的voterhistory
记录列中,如下所示: 2011, 2012, 2013
。
Sorry if this sounds confusing, but it's the only way I know how to explain it. 抱歉,这听起来令人困惑,但这是我知道如何解释的唯一方法。 If you need further information please let me know.
如果您需要更多信息,请告诉我。
In MySQL we have a function called CONCAT_WS() It basically concatenates string values using a specified separator. 在MySQL中,我们有一个名为CONCAT_WS()的函数,它基本上使用指定的分隔符来连接字符串值。 Take a look at the link.
看一下链接。
I would use a temp table to load vhist into memory.Then join table week1 to the temp table vhistory and update week1 based on the key matches. 我将使用一个临时表将vhist加载到内存中,然后将表week1连接到临时表vhistory并根据键匹配更新week1。 That gets you out of having to implement conditional logic in your SQL join.
这使您不必在SQL连接中实现条件逻辑。
In code it will look something like this, you may have to work on this a bit to get it to run: 在代码中它将看起来像这样,您可能需要对此稍作努力才能使其运行:
CREATE TEMPORARY TABLE vhistory (
voternum VARCHAR(10) NOT NULL
, voterhistory VARCHAR(10)
);
LOAD DATA LOCAL INFILE '/path/vhist.txt' INTO TABLE vhistory;
UPDATE week1 SET voterhistory=SELECT CONCAT_WS(SELECT ',',voterhistory FROM vhistory
WHERE week1-voternum = vhistory-voternum );
I did not run this code as I don't have your data. 我没有您的数据,因此未运行此代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.