在现有表中创建新列并使用python更新列

时间:2018-08-22 13:13:20

标签: python mysql pandas

在这里,我试图将一个表与具有数百万个记录的2个表进行比较,所以我想分别比较dem并在主表“ tabl”名称状态中创建一个单独的列,以便我可以对其本身进行更新

步骤:在主表中的电子邮件名称“状态”旁边创建一个新列

step2 :必须在与tab1,tab2表进行比较时更新该列

list_of_tables=['tab1','tab2'] 
for tab in list_of_tables:
    cursor.execute("select main.*,if({}.email is not null ,'MATCH','NONMATCH') stataus from main left join {} on main.email={}.email".format(tab,tab,tab))
     data_2 = cursor.fetchall()
     print data_2
     data3=list(data_2)
     data_3=pd.DataFrame(data3)
     upload(ftp,data_3,FILEPATH)

def upload(ftp,data_3,FILEPATH):
    data_4=data_3.to_csv(Out_file,index=False,header=None)


main:

email
abc@gamil.com
xyz@email.com
ijk@gmail.com
ghi@gmail.com
pqr@gmail.com
yup@gmail.com
tab1:

email
ijk@gmail.com
yup@gmail.com
tab2:

email
xyz@email.com
pqr@gmail.com

所需结果

email           valid
abc@gamil.com   non-match
xyz@email.com   match
ijk@gmail.com   match
ghi@gmail.com   non-match
pqr@gmail.com   match
yup@gmail.com   match

but getting like dis:

abc@gamil.com       non-match
xyz@email.com       non-match
ijk@gmail.com       match
ghi@gmail.com       non-match
pqr@gmail.com       non-match
yup@gmail.com       match
abc@gamil.com       nonmatch
xyz@email.com       match
ijk@gmail.com       nonmatch
ghi@gmail.com       nonmatch
pqr@gmail.com       nonmatch
yup@gmail.com       nonmatch

2 个答案:

答案 0 :(得分:0)

dct[x.group(1)]

查询很好,但是我希望for循环运行出现问题,因为我得到的是最后一个tab2比较结果,而不是tab1和tab2都是比较结果。

Scott Boston的更新:

您仍然可以运行:

<form action="storeupdate.php" enctype="multipart/form-data" method="post">
<input type='checkbox' name='Tags[]' value='Milk' >Milk<br/>
<input type='checkbox' name='Tags[]' value='Sugar' >Sugar<br/>
<input type='checkbox' name='Tags[]' value='Cream' >Cream<br/>
<input type='checkbox' name='Tags[]' value='Chocolate' >Chocolate<br/>
<input type="reset"> <input type="submit" name="Submit" value="Submit"><br/>
</form>

答案 1 :(得分:0)

终于明白了:

data3 = data2.to_sql('main', con=engine, if_exists='replace',index=False)
    cursor.execute("alter table main add status varchar(128) **not null default 'nonmatch**'")
    d=cursor.fetchall()
    print d

    for i in ['tab1','tab3','tab2']:
        cursor.execute("update main **join** {} on main.email={}.email set status=if({}.email is not null,'match','nonmatch')".format(i,i,i))
        conn.commit()