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

时间:2018-04-26 14:25:45

标签: 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 union查询语法。谢谢。

1 个答案:

答案 0 :(得分:0)

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