带有LIKE运算符错误的WHERE子句

时间:2014-10-03 06:35:15

标签: where-clause sql-like

我想使用WHERE ... LIKE ...语句来搜索两个父表,即: t_persons t_incidents

INNER JOIN工作正常,但是当我包含WHERE ... LIKE ...语句时,不会返回任何记录。

另外,当我在表单中输入一个关键词来搜索时,例如FamilyName LIKE' mulenga'和IncidentCountryID LIKE',我打印出这个错误:

SELECT ip.PersonID, ip.IncidentID, p.ImagePath, p.FamilyName, p.FirstName, p.OtherNames, p.Gender, p.CountryID, i.IncidentDate, i.IncidentCountryID, i.AgencyID, i.StatusID, i.KeywordID FROM t_incidents i INNER JOIN t_incident_persons ip ON ip.IncidentID = i.IncidentID INNER JOIN t_persons p ON ip.PersonID = p.PersonID WHERE p.FamilyName LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND p.FirstName LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND p.OtherNames LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND p.Gender LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND i.IncidentDate LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND i.KeywordID LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND i.IncidentCountryID LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND i.StatusID LIKE '% AND `FamilyName` LIKE '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND ip.PersonID>0 ORDER BY PersonID DESC LIMIT 0,24You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%mulenga%' AND `IncidentCountryID` LIKE '%243%'%' AND p.FirstName LIKE '% AND ' at line 12

我的SELECT语句如下:

$query = "SELECT ip.PersonID, ip.IncidentID, p.ImagePath, p.FamilyName, p.FirstName,
  p.OtherNames, p.Gender, p.CountryID, i.IncidentDate, i.IncidentCountryID,
  i.AgencyID, i.StatusID, i.KeywordID

FROM t_incidents i 
INNER JOIN
  t_incident_persons ip ON ip.IncidentID = i.IncidentID 
INNER JOIN
  t_persons p ON ip.PersonID = p.PersonID    

  WHERE 
    p.FamilyName LIKE '%" . $likes . "%' AND
    p.FirstName LIKE '%" . $likes . "%' AND
    p.OtherNames LIKE '%" . $likes . "%' AND
    p.Gender LIKE '%" . $likes . "%' AND
    i.IncidentDate LIKE '%" . $likes . "%' AND
    i.KeywordID LIKE '%" . $likes . "%' AND
    i.IncidentCountryID LIKE '%" . $likes . "%' AND
    i.StatusID LIKE '%" . $likes . "%' AND
    ip.PersonID>0

ORDER BY PersonID DESC $pages->limit";

我的表格设置如下( t_incident_persons 是联结表格):

t_persons
+-----------+-----------+----------+----------+
| PersonID  | FamilyName| FirstName| CountryID|
+-----------+-----------+----------+----------+
|         1 | Bupe      | Joseph   | 243      |
|         2 | Kansi     | Brown    | 243      |
|         3 | Luma      | Henry    | 243      |
+-----------+-----------+----------+----------+

t_incidents
+--------------+-------------------+-------------+----------------+--------+--------+
| IncidentID   | IncidentCountryID |IncidentDate |OffenceKeywordID|AgencyID|StatusID|
+--------------+-------------------+-------------+----------------+--------+--------+
| 1            | 243               |12/03/2003   |1               |6       |2       |   
| 2            | 243               |15/05/2004   |1               |6       |2       |
| 3            | 243               |16/12/2007   |3               |6       |3       |
+--------------+-------------------+-------------+----------------+--------+--------+

t_incident_persons
+-----------+----------+
| IncidentID| PersonID |
+-----------+----------+
| 1         | 1        |
| 1         | 2        |
| 1         | 3        |
+-----------+----------+

0 个答案:

没有答案