因为有个需求需要每天按时导表,我懒得天天手动导,希望能用 python 来实现导表+发邮件的工作 然而我就是个做表的,对 python 的了解也就限于安装与卸载这种程度…所以在编写的时候遇到了很多麻烦 我现在就卡在了 sql 查询有语法错误上,同样的查询,放在 navicat 和 excel 里都是能正常查询的 然而不知道为什么 python 就总是说"you have an error in your SQL syntax" ╮(╯ _╰)╭ 希望各位 CS dalao 能帮我看一下是哪里写的不对,靴靴 import pymysql conn = pymysql.connect(host='****', port=3307,user='****',passwd='****',db='autocar',charset='UTF8') cur = conn.cursor() sql = "SELECT\ contract.contract_number AS 合同编号,\ apply.into_time AS 进件时间,\ contract.actual_loan_time AS 放款确认时间,\ apply_detail.city_manager AS 城市经理,\ apply_detail.marketing_manager AS 市场经理,\ apply_detail.sales_name AS 销售姓名,\ apply.org_name AS 分公司,\ apply.product_name AS 产品名称,\ apply.loan_term AS 借款期限,\ contract.loan_amount AS 合同金额,\ n.statusdes AS 实时状态\ FROM apply\ LEFT JOIN contract ON contract.apply_id = apply.apply_id\ LEFT JOIN apply_detail ON apply_detail.apply_id = apply.apply_id\ LEFT JOIN (\ SELECT\ group_concat(DISTINCT c.status_code SEPARATOR '||') AS statussum,\ c.is_in_node,\ c.apply_id,\ group_concat(DISTINCT c.status_name SEPARATOR '||') AS statusdes,\ group_concat(DISTINCT c.operator_name SEPARATOR '||') AS operatornames\ FROM\ node_record c\ WHERE\ c.is_in_node = 1\ AND c.is_valid = 1\ GROUP BY c.apply_id\ ) n ON apply.apply_id = n.apply_id\ WHERE\ 1 = 1\ AND apply.is_ex_apply = '1'\ AND apply.borrowing_type = '1'\ AND apply.into_time >= '2016-11-1'\ AND apply_detail.sales_name NOT LIKE '%测试%'" cur.execute(sql) for i in cur: print(i) cur.close() conn.close()
>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'applyLEFT JOIN contract ON contract.apply_id = apply.apply_idLEFT JOIN apply_det' at line 1
apply 和 LEFT JOIN 之间少了空格。这么长的字符串直接用三引号"""str""",会保留所有回车和空格,不用加这么多斜杠。