python - Pymysql 使用 %s (参数占位符)执行联合查询时

标签 python mysql database pymysql

enter image description here

这是关于UNION QUERY的代码:

        smith ='Smith'
        smithb='Smith'

        sql="""SELECT Distinct Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum = Dnumber AND Mgr_ssn=Ssn AND Lname= %s 
        UNION SELECT Distinct Pnumber PROJECT, WORKS_ON, EMPLOYERE WHERE Pnumber =Pno AND Essn=Ssn AND Lname=%s"""

        curs.execute(sql,(smith,smithb))
        #curs.execute(sql2,('Smith'))
        conn.commit()     
        rows = curs.fetchall()
        print(rows)

这是错误消息!!:

pymysql.err.ProgrammingError: (1064, "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 'WHERE Pnumber =Pno AND Essn=Ssn AND Lname='Smith'' at line 1")

我需要知道 pymysql 联合查询语法。谢谢。

最佳答案

尝试将 Lname= %s 更改为 Lname LIKE '%s'

关于python - Pymysql 使用 %s (参数占位符)执行联合查询时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045516/

相关文章:

python - 在 Windows 10 (python 3.6.2) 中安装 Tensorflow 时出现问题

python - 如何使用 hmmlearn 在 Python 中运行隐马尔可夫模型?

php - 将数据从 js 发送到 php、php 到 mysql 以及 REST json 响应时所需的正确编码/转义/htmlentities 是什么

mysql - 数据库归一化 2NF 和 3NF

php - CodeIgniter 中 Active Record 的 "count_all_results"和 "where"问题

python - 如何在 django 的日历应用程序中找到 "week"?

Python:如何用一系列数字填充数组?

php - codeigniter,如何获取多个查询并从模型中对其进行计数

sql-server - 数据库代码不再有效

mysql - 获取 MySql 数据库条目的近似值