MySQL 使用 IN、LIKE 和 OR
MySQL Use of IN, LIKE and OR
我正在尝试编写一个搜索算法,没什么太高级的,但它不仅仅是 WHERE field1
= 'searchtext'。我正在尝试跨多个字段搜索所有关键字。
我进行了一些搜索,似乎我对此事的看法不符合 MySQL 对 'IN' 与其他功能的使用,但是我找不到任何符合要求的内容似乎在 Whosebug 上或在独立博客和其他教程网站上使用 google 提出了更好的方法。
$fields = array('type','suburb','postcode','address'); // Fields in db being searched
$queried = $db->real_escape_string($_REQUEST['keyword']); // Input from form
$keys = explode(" ",$queried); // Determine individual keywords
$sql = "SELECT * FROM `properties` WHERE "; // Beginning of SQL Statement
$frc = 0; // Field Counter
foreach($fields as $f){
$inner = ''; // Reset $inner each run
$irc = 0; // Reset Inner Counter each run
$frc++; // Increase Field Counter
if($frc != 1){ $sql .= " OR "; } // All except first runthrough
$sql .= "`".$f."` IN "; // `field` IN
foreach($keys as $k){
$irc++; // Increase inner counter
if($irc == 1){
$inner .= "('%".$k."%'"; // First Inner per run (aka each keyword)
}else{
$inner .= ", '%".$k."%'"; // All other Inners
}
}
$inner .= ")"; // Inner finishes run before reset
$sql .= $inner; // Add Inner to SQL ready for query
}
$sql .= ";"; // Clean finish to SQL statement
$SearchProperties = $db->query($sql); // Run Query
我加入了评论,以帮助您理解我混乱的代码以及我的感受。代码给出了预期的输出,例如,如果我搜索关键字 "house",我的输出如下;
$queried = house 3064
$sql = SELECT * FROM `properties` WHERE `type` IN ('%house%', '%3064%') OR `suburb` IN ('%house%', '%3064%') OR `postcode` IN ('%house%', '%3064%') OR `address` IN ('%house%', '%3064%');
type
栏内有house和townhouse,应该都可以打,邮编3064的都应该打,不管其他栏有没有house(按我的要求完成)
然而,经过几个小时的搜索,虽然我的输出符合要求,但我不认为它是正确的。任何人都可以帮助阐明解决我的问题的正确方法以及为什么这不起作用?我总是喜欢从这些误解中去理解和学习。
感谢您的帮助。
如果你有通配符,你需要 like
而不是 in
:
SELECT *
FROM `properties`
WHERE (`type` LIKE '%house%') OR
(`suburb` LIKE '%house%') OR
(`postcode` LIKE '%house%') OR
(`address` LIKE '%house%');
但是,我强烈建议您研究一下全文索引(参见 here)。使用 MATCH()
可以大大简化您的工作。
编辑:
您的查询仍然不正确。你应该仍然使用 like
:
SELECT *
FROM `properties`
WHERE (`type` LIKE '%house%' or type like '%3064%') OR
(`suburb` LIKE '%house%' or suburb like '%3064%') OR
(`postcode` LIKE '%house%' or postcode like '%3064%') OR
(`address` LIKE '%house%' or address like '%3064%');
尝试将 'IN' 更改为 'LIKE'。
例如
$queried = house
$sql = SELECT * FROM `properties` WHERE
`type` LIKE '%house%'
OR `suburb` LIKE '%house%'
OR `postcode` LIKE '%house%'
OR `address` LIKE '%house%';
如果您有多个关键字,则需要更改查询。
例如
$queried = house 3064
$sql = SELECT * FROM `properties` WHERE
(`type` LIKE '%house%' AND `type` LIKE '%3064%')
OR (`suburb` LIKE '%house%' AND `suburb` LIKE '%3064%')
OR (`postcode` LIKE '%house%' AND `postcode` LIKE '%3064%')
OR (`address` LIKE '%house%' AND `address` LIKE '%3064%');
我正在尝试编写一个搜索算法,没什么太高级的,但它不仅仅是 WHERE field1
= 'searchtext'。我正在尝试跨多个字段搜索所有关键字。
我进行了一些搜索,似乎我对此事的看法不符合 MySQL 对 'IN' 与其他功能的使用,但是我找不到任何符合要求的内容似乎在 Whosebug 上或在独立博客和其他教程网站上使用 google 提出了更好的方法。
$fields = array('type','suburb','postcode','address'); // Fields in db being searched
$queried = $db->real_escape_string($_REQUEST['keyword']); // Input from form
$keys = explode(" ",$queried); // Determine individual keywords
$sql = "SELECT * FROM `properties` WHERE "; // Beginning of SQL Statement
$frc = 0; // Field Counter
foreach($fields as $f){
$inner = ''; // Reset $inner each run
$irc = 0; // Reset Inner Counter each run
$frc++; // Increase Field Counter
if($frc != 1){ $sql .= " OR "; } // All except first runthrough
$sql .= "`".$f."` IN "; // `field` IN
foreach($keys as $k){
$irc++; // Increase inner counter
if($irc == 1){
$inner .= "('%".$k."%'"; // First Inner per run (aka each keyword)
}else{
$inner .= ", '%".$k."%'"; // All other Inners
}
}
$inner .= ")"; // Inner finishes run before reset
$sql .= $inner; // Add Inner to SQL ready for query
}
$sql .= ";"; // Clean finish to SQL statement
$SearchProperties = $db->query($sql); // Run Query
我加入了评论,以帮助您理解我混乱的代码以及我的感受。代码给出了预期的输出,例如,如果我搜索关键字 "house",我的输出如下;
$queried = house 3064
$sql = SELECT * FROM `properties` WHERE `type` IN ('%house%', '%3064%') OR `suburb` IN ('%house%', '%3064%') OR `postcode` IN ('%house%', '%3064%') OR `address` IN ('%house%', '%3064%');
type
栏内有house和townhouse,应该都可以打,邮编3064的都应该打,不管其他栏有没有house(按我的要求完成)
然而,经过几个小时的搜索,虽然我的输出符合要求,但我不认为它是正确的。任何人都可以帮助阐明解决我的问题的正确方法以及为什么这不起作用?我总是喜欢从这些误解中去理解和学习。
感谢您的帮助。
如果你有通配符,你需要 like
而不是 in
:
SELECT *
FROM `properties`
WHERE (`type` LIKE '%house%') OR
(`suburb` LIKE '%house%') OR
(`postcode` LIKE '%house%') OR
(`address` LIKE '%house%');
但是,我强烈建议您研究一下全文索引(参见 here)。使用 MATCH()
可以大大简化您的工作。
编辑:
您的查询仍然不正确。你应该仍然使用 like
:
SELECT *
FROM `properties`
WHERE (`type` LIKE '%house%' or type like '%3064%') OR
(`suburb` LIKE '%house%' or suburb like '%3064%') OR
(`postcode` LIKE '%house%' or postcode like '%3064%') OR
(`address` LIKE '%house%' or address like '%3064%');
尝试将 'IN' 更改为 'LIKE'。 例如
$queried = house
$sql = SELECT * FROM `properties` WHERE
`type` LIKE '%house%'
OR `suburb` LIKE '%house%'
OR `postcode` LIKE '%house%'
OR `address` LIKE '%house%';
如果您有多个关键字,则需要更改查询。 例如
$queried = house 3064
$sql = SELECT * FROM `properties` WHERE
(`type` LIKE '%house%' AND `type` LIKE '%3064%')
OR (`suburb` LIKE '%house%' AND `suburb` LIKE '%3064%')
OR (`postcode` LIKE '%house%' AND `postcode` LIKE '%3064%')
OR (`address` LIKE '%house%' AND `address` LIKE '%3064%');