1

我想使用该OR子句搜索 3 列。我知道,我只是遗漏了一些东西,但我不断收到 mysql 错误。

这是我到目前为止所拥有的:

SELECT customerid,companyname,firstname,lastname
FROM `tbl_customers` 
WHERE customeronoff = 1 
AND customerrecordtype = 'A' 
AND (companyname LIKE '$query%') OR (lastname LIKE '$query%') OR (firstname LIKE '$query%')   
ORDER BY lastname ASC

我一定是把 OR 错了。有人能帮我修一下吗

4

2 回答 2

3

使用 paranethesis 对您的ORstaements 进行分组:

AND (
     (companyname LIKE '$query%') 
  OR (lastname LIKE '$query%') 
  OR (firstname LIKE '$query%')  
)
于 2013-01-23T19:51:11.357 回答
1

您缺少两个括号:

SELECT customerid,companyname,firstname,lastname
FROM `tbl_customers` 
WHERE customeronoff = 1 
AND customerrecordtype = 'A' 
AND **(**(companyname LIKE '$query%') OR (lastname LIKE '$query%') OR (firstname LIKE '$query%')**)**
ORDER BY lastname ASC
于 2013-01-23T19:52:11.560 回答