SQL错误代码:#1064

时间:2014-08-29 08:20:21

标签: php mysql sql

做一些SQL并试图安排我的数据。我做了代码等但是我得到了#1064错误(语法问题)有人可以检查我的代码并告诉我哪里错了:)

 $database = new connection();
    $database = $database->Connect("companyhouse");

    $statement = $database->prepare(" SELECT * 
                                      FROM companyhouse_23 
                                      ORDER BY Incorporation_Date DESC
                                      WHERE 
                                        incorporation_date >= NOW() - INTERVAL 3 MONTH
                                        AND County = 'Sussex'
                                        AND County LIKE '%Sussex%'
                                      ");
    $statement->execute();
    $Results = $statement->fetchall(PDO::FETCH_OBJ);

在我看来,这一切都很好,但显然不是吗?哈哈

4 个答案:

答案 0 :(得分:2)

您之前使用Order By子句而不是Where子句。 Order By子句必须是任何查询的最后一个子句。说得对。

答案 1 :(得分:1)

 $statement = $database->prepare(" SELECT 
                                         * 
                                      FROM 
                                         companyhouse_23 
                                      WHERE 
                                        incorporation_date >= NOW() - INTERVAL 3 MONTH
                                        AND County = 'Sussex'
                                        AND County LIKE '%Sussex%'
                                      ORDER BY 
                                         Incorporation_Date DESC
                                      ");

答案 2 :(得分:1)

像这样重新排列查询。 ORDER BY之后应使用WHERE

SELECT 
    * 
FROM 
    companyhouse_23 
WHERE 
   incorporation_date >= NOW() - INTERVAL 3 MONTH
   AND County = 'Sussex'
   AND County LIKE '%Sussex%'
ORDER BY 
    Incorporation_Date DESC

答案 3 :(得分:0)

SELECT * 
FROM  companyhouse_23 
WHERE incorporation_date >= NOW() - INTERVAL 3 MONTH 
   AND  County='Sussex' 
   AND County LIKE '%Sussex%' 
   ORDER BY Incorporation_Date DESC

您的查询中需要注意的两件事:

  1. 排序依据应该是您查询的最后一部分。
  2. 在你的where子句中你使用county ='Susses'和county就像'%Sussex%'(这个条件只会返回有county ='Sussex'的行(冗余like子句) 根据您的要求仅使用一个(如果完全匹配是reqd use = else Like子句)