我想合并数据。以下是我的MySQL表。我想用Python遍历这两个列表(其中一个用dupe ='x',另一个用null dupes)。如何比较2个列表并将它们合并到Python/MySQL中?
这是示例数据。实际数据是巨大的。
例如:
a b c d e f key dupe
--------------------
1 d c f k l 1 x
2 g h j 1
3 i h u u 2
4 u r t 2 x
从上面的示例表,所需的输出是:
a b c d e f key dupe
--------------------
2 g c h k j 1
3 i r h u u 2
我到目前为止有:
import string, os, sys
import MySQLdb
from EncryptedFile import EncryptedFile
enc = EncryptedFile(os.getenv("HOME") + '/.py-encrypted-file')
user = enc.getValue("user")
pw = enc.getValue("pw")
db = MySQLdb.connect(host="127.0.0.1", user=user, passwd=pw,db=user)
cursor = db.cursor()
cursor2 = db.cursor()
cursor.execute("select * from delThisTable where dupe is null")
cursor2.execute("select * from delThisTable where dupe is not null")
result = cursor.fetchall()
result2 = cursor2.fetchall()
for each record
for each field
perform the comparison and perform the necessary updates
### How do I compare the record with same key value and update the original row null field value with the non-null value from the duplicate? Please fill this void...
cursor.close()
cursor2.close()
db.close()
谢谢你们!
想不通的问题。你想获得algorythm,还是根据具体框架来实现? 事实上,你不需要遍历游标和'coalesce'项的字段。 在这种情况下你可以执行普通的SQL吗?如果可以,原因是查询很简单。 – 2010-04-09 20:26:21
这是简单,简单的测试数据。实际上,有几千行和几百列,因此这种方法。谢谢。 – ThinkCode 2010-04-09 20:33:14
update delthistable t set ta = coalesce(dup.a,ta),tb = coalesce(dup.b,tb)... from(select * from delthistable where dupe ='x')dup 其中t.dupe <>'x'and t.key = dup.key ------------------------------------ -------------------------- 从delthistable删除其中dupe <>'x' – 2010-04-09 20:51:51