SQL 比较运算符 Python) 我无法通过 python 文件对 SQL 查询执行比较运算符

SQL Comparison Operator Python) I can't execute comparison operators to SQL query through python file

提问人:koguma 提问时间:6/20/2021 更新时间:6/20/2021 访问量:30

问:

sql = "WITH comparing_price AS (SELECT CODE, DATE, OPEN, high, low, close, volume,"\
                "LEAD(OPEN, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_open',"\
                "LEAD(OPEN, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_open',"\
                "LEAD(OPEN, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_open',"\
                "LEAD(high, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_high',"\
                "LEAD(high, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_high',"\
                "LEAD(high, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_high',"\
                "LEAD(high, 4) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd4_high',"\
                "LEAD(low, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_low',"\
                "LEAD(low, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_low',"\
                "LEAD(low, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_low',"\
                "LEAD(close, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_close',"\
                "LEAD(close, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_close',"\
                "LEAD(close, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_close',"\
                "LEAD(volume, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_volume',"\
                "LEAD(volume, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_volume',"\
                "AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 2 preceding AND 2 following) AS 'd2_MA5',"\
                "AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 7 preceding AND 2 following) AS 'd2_MA10',"\
                "AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_MA20',"\
                "AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 57 preceding AND 2 following) AS 'd2_MA60',"\
                "AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 117 preceding AND 2 following) AS 'd2_MA120',"\
                "STD(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_std'"\
            "FROM daily_price)"\
        "SELECT * "\
        "FROM comparing_price"\
        "WHERE "\
            "volume > 1 AND d1_volume > 1 AND d2_volume > 1"
execute(sql)

我收到一条错误消息:

pymysql.err.ProgrammingError: 
(1064, "You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the right syntax to use near 
'> 1 AND d1_volume > 1 AND d2_volume > 1' at line 1")

我认为问题在于通过execute()发送比较运算符,因为在HeidiSQL中直接运行此查询时,它工作正常。或者有没有其他想法为什么它可能不起作用?

python sql 比较 mysql-error-1064

评论

1赞 Serg 6/20/2021
看起来空间丢失导致comparing_priceWHERE
0赞 6/20/2021
三引号字符串将帮助您避免类似的错误。
0赞 Gordon Linoff 6/20/2021
@Koguma . . .将来,打印出 SQL 字符串并尝试运行实际字符串。
0赞 koguma 6/20/2021
哦,天哪,非常感谢你们!它现在起作用了!我应该仔细检查一下......不好意思。我以为这是因为我对 SQL 一无所知,但看起来我应该仔细检查我的语法。谢谢!!!

答: 暂无答案