简体   繁体   English

php / mysql搜索多个值

[英]php/mysql search for multiple values

I have a table homes with (country,city,area,published) fields. 我有一个带有(国家,城市,区域,已发布)字段的表房屋。 I have a search form where someone can enter country or city or area. 我有一个搜索表,有人可以输入国家,城市或地区。 I want to get all homes which are published = 1 and any of the search terms matches any of their fields. 我想获取所有已发布的房屋= 1,并且任何搜索词都与它们的任何字段匹配。

This is what I have so far: 这是我到目前为止的内容:

SELECT * FROM homes 
WHERE published = 1 
AND 
LOWER(country) LIKE '$search%' 
OR 
LOWER(city) LIKE '$search%'
OR
LOWER(area) LIKE '$search%'

The problem is that it return homes that have published = 0... 问题在于它返回已发布的房屋= 0 ...

I would suggest parentheses. 我建议用括号。

SELECT * FROM homes 
WHERE published = 1 
AND
(
 LOWER(country) LIKE '$search%' 
 OR 
 LOWER(city) LIKE '$search%'
 OR
 LOWER(area) LIKE '$search%'
)
SELECT * 
FROM   homes 
WHERE  published = 1 
       AND ( country LIKE '$search%' 
              OR city LIKE '$search%' 
              OR area LIKE '$search%' ) 

Like already does the case-insensitive stuff so you don't need it. 就像已经不区分大小写的东西一样,因此您不需要它。

The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. 默认字符集和排序规则为latin1和latin1_swedish_ci,因此默认情况下非二进制字符串比较不区分大小写。 This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. 这意味着,如果使用col_name LIKE'a%'搜索,则将获得以A或a开头的所有列值。 http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM