[英]Python MySQL, 1064 (42000), "You have an error in your SQL syntax;..."
This's my table test_table
:这是我的表
test_table
:
id name身份证名称
1 aa 1个氨基酸
2 bb 2BB
3 cc 3毫升
I get the message when it's run with python3:使用 python3 运行时我收到消息:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test as(SELECT * from test_table) SELECT name FROM test' at line 1
检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“test as(SELECT * from test_table) SELECT name FROM test”附近使用正确的语法
this is my syntax:这是我的语法:
cnxn = mysql.connector.connect(**sql_config)
cursor = cnxn.cursor()
query = """
with test as(SELECT * FROM `test_table`)
SELECT * FROM test
"""
cursor.execute(query)
get_data = cursor.fetchall()
cnxn.close()
cursor.close()
The bug may be in syntax of query.该错误可能在查询语法中。
Because I tried the following two methods with without error:因为我尝试了以下两种方法而没有错误:
1.Sequel Ace: use 'with' 1.Sequel Ace:使用'with'
with `test` as ( SELECT * FROM `test_table`)
SELECT `name` FROM `test`
2.python3: not used 'with' and can work 2.python3:不使用'with'并且可以工作
query = """
SELECT name FROM test_table
"""
How do I modify 'with as' of syntax?如何修改“with as”的语法?
query = """
with test as(SELECT * FROM test_table)
SELECT * FROM test
"""
I'm almost sure you're on an old version of mysql.我几乎可以肯定您使用的是旧版本的 mysql。 The Common table expression feature (that one with "with") was introduce only in version 8.
公用表表达式功能(带有“with”的功能)仅在版本 8 中引入。
Every version below 8 will return you exact same error message.低于 8 的每个版本都会返回完全相同的错误消息。 See first db_fiddle
见第一个db_fiddle
Whereas the same query runs perfect starting from version 8 as you may seen on the second db_fiddle example正如您在第二个db_fiddle 示例中所见,相同的查询从版本 8 开始运行完美
But that's not the end.但这还不是结束。 The CTE can be replaced with a subquery like this
CTE 可以用这样的子查询替换
SELECT name FROM (select 'XXX' name) q;
or, in case, if you need to join:或者,如果您需要加入:
select x.name
FROM (select 'XXX' name) x
JOIN (select 'XXX' name union all select 'YYY') y
ON x.name = y.name;
as you can see on third db_fiddle example正如您在第三个 db_fiddle 示例中看到的那样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.