Mysql语法错误,1064

时间:2014-12-09 14:50:24

标签: mysql sql

我正在尝试在mysql中使用条件选择,但是我遇到了语法错误。你能看到错误吗?

IF EXISTS (
    SELECT 1 FROM `proxies` 
    WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and `PAYMENT`='sharedproxies' and `connections`<3;
    )
    THEN SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and `PAYMENT`='sharedproxies' and `connections`<3 limit 1;
ELSEIF EXISTS (
    SELECT 1 FROM `proxies` 
    WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and (`tier`='1' or `tier`='2') and `PAYMENT`='scanner' and `connections`<3
    )
    THEN SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and (`tier`='1' or `tier`='2') and `PAYMENT`='scanner' and `connections`<3 limit 1

#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 'IF EXISTS (SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 8640' at line 1 

2 个答案:

答案 0 :(得分:0)

MySQL错误消息#1064 - You have an error in your SQL syntax总是很清楚。在消息'...正确的语法使用附近'之后,它显示了它无法理解的查询部分。

在您的情况下,MySQL不理解的部分从查询的最开始开始。 这是因为“IF EXISTS(”(和“ELSEIF EXISTS”和“THEN”)是无效的MySQL语法。

最简单的解决方法是运行第一个查询:

SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and `PAYMENT`='sharedproxies' and `connections`<3 limit 1

检查客户端代码是否返回了什么,如果没有,则运行第二个查询:

SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and (`tier`='1' or `tier`='2') and `PAYMENT`='scanner' and `connections`<3 limit 1

您也可以这样做:

(SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and `PAYMENT`='sharedproxies' and `connections`<3 limit 1)
UNION ALL
(SELECT * FROM `proxies` WHERE `last_used1/1`<(UNIX_TIMESTAMP() - 86400) and `status`='active'  and (`tier`='1' or `tier`='2') and `PAYMENT`='scanner' and `connections`<3 limit 1)

然后,在客户端代码中检查查询返回的行的字段,以找出第一个子查询返回的行以及第二个子查询返回的行(假设它返回2行)。 / p>

有关UNION / UNION ALL以及如何修改上述查询以直接从MySQL获取所需内容的详情,请参阅documentation

答案 1 :(得分:-1)

ELSEIF必须是:ELSE IF

我认为这是错误。你不能在www.google.com上找到它吗?