1

我正在尝试使 MySQL 选择用于 mysql_query 函数,但我不断收到此错误:

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 'LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID 
            LEFT JOIN Persons ON Jobs.CustPer' at line 11

我已经尝试了所有方法并到处搜索,但似乎没有任何效果。感谢所有帮助。

 $Qry = "SELECT 

     Jobs.ID, 
     'Jobs.Status',
     Jobs.JobNum, 
     'Orgs.Nme', 
     'Persons.FirstNme',
     'Persons.LastNme',
     'JobTypes.JobType',
     'Jobs.Dsc',
     'Jobs.Notes'
     FROM Jobs ";

     if($column !== null && $text !== null) {
        $Qry .= "WHERE " . $column . " LIKE '%" . $text . "%' ";
     }

     $Qry .= "LEFT JOIN Orgs ON Jobs.CustOrgID = Orgs.ID 
         LEFT JOIN Persons ON Jobs.CustPersonID = Persons.ID
         LEFT JOIN JobTypes ON Jobs.JobTypeID = JobTypes.ID 
         ORDER BY JobNum";

解决方案:

     SELECT ...
     FROM ...
     LEFT JOIN ...
     LEFT JOIN ...
     WHERE ...
     ORDER BY ...

我的 WHERE 在错误的地方,它应该出现在两个 LEFT JOINS 之后。

4

2 回答 2

5

您将 WHERE 插入错误的位置。它必须在连接之后:

SELECT ...
FROM ...
LEFT JOIN ...
LEFT JOIN ...
WHERE ...
ORDER BY ...
于 2013-07-19T19:22:47.447 回答
3

您应该使用反引号而不是单引号,或者如果它们不是保留关键字或分隔字符串,则不必使用反引号。

尝试这个 :

     $Qry = "SELECT 

 `Jobs`.`ID`, 
 `Jobs`.`Status`,
 `Jobs`.`JobNum`, 
 `Orgs`.`Nme`, 
 `Persons`.`FirstNme`,
 `Persons`.`LastNme`,
 `JobTypes`.`JobType`,
 `Jobs`.`Dsc`,
 `Jobs`.`Notes`
 FROM Jobs ";
于 2013-07-19T19:22:35.523 回答