使用python脚本比较两个表

时间:2018-02-26 14:36:34

标签: python mysql

我有两张桌子

第一张表:这里有更多数据,如

select PROJ_No from INSProjectDataStaging where PROJ_NO>='7000-00'
(only this particular records will be present in second table)

第二个表:需要检查数据是否存在于此表中的记录。在这里我可以从db获取数据,但我需要添加一个条件if PROJ_NO<7000无效项目以及其他有效的项目编号 这是我的python代码

cursor.execute(qstr):
    try:
        result=cursor.fetchall()
        col_names=[i[0] for i in cursor.description]
        for row in result:
            row_dict = dict(zip(col_names,row))
            new_row = list()
            for k,v in row_dict.iteritems():
                new_row.append(str(k)+"."+str(v))
            Databaserecords.append(new_row)
            for k.LOG_PROJ_NO in qstr:
                if k.LOG_PROJ_NO<'7000-00':
                    print "invalid project number existed"
    except Exception as e:
        print "problem in getting records from DatabaseTable with the query\n",qstr,"\nDetails:-",e

compare("select Activity_Code,PROJ_NO,LOG_PROJ_NO,LOG_DESC from table1 inner join table2 on table1.PROJ_NO=table2.LOG_PROJ_NO")

但输出显示所有记录都存在无效项目编号,我需要输出,如果存在无效项目编号以及其他有效记录,则必须在额外列中显示无效记录 请帮助解决这个问题

0 个答案:

没有答案