如何使用LIKE正确编写以下INSERT?
provider = provider
cursor.execute("""INSERT INTO raw_financials (provider, vendor_id)
VALUES (%s, %s)""",
((SELECT provider FROM main_app_provider
WHERE provider LIKE %s%, %provider), vendor_id)
例如,使用上面的LIKE语句,“Apple”将匹配“Apple Inc.”。
答案 0 :(得分:3)
使用INSERT命令的INSERT ... SELECT ... FROM
variant:
sql = """
INSERT INTO raw_financials (provider, vendor_id)
SELECT provider, %s
FROM main_app_provider
WHERE provider LIKE %s
"""
args = (vendor_id, '%'+provider+'%')
cursor.execute(sql, args)