php - MySql 搜索两个组合字段的查询

标签 php mysql search

我正在开发一个允许用户列出待售船只和游艇的网站。有一个 mysql 数据库,其中有一个表“yachts”,其他字段包括“make”和“model”。

当人们访问网站寻找待售船只时,会出现一个搜索表单,其中一个选项是在文本字段中输入品牌和/或型号。结果页面上的相关 where 子句如下

WHERE ( make LIKE '%$yacht_make%' OR model  LIKE '%$yacht_make%') 

如果有人输入品牌模型,则此方法有效,但如果他们同时输入两者,则无效。

例如,如果有人输入品牌“Jeanneau”,它会找到该品牌的船,或者如果他们输入型号“Sun Odyssey”,它会找到该型号的船,但如果他们输入“Jeanneau” Sun Odyssey”它空空如也。

有没有一种方法可以编写一个查询,其中输入上述搜索条件的所有三种方式都可以找到船?

这是网站 http://yachtsoffered.com/

谢谢

罗布·芬威克

编辑:

查询是用 php 脚本构建的,这里是脚本

    if(!empty($yacht_id)) {
    $where = " WHERE yacht_id = $yacht_id ";
} else {
    $where = " WHERE ( make LIKE '%$yacht_make%' OR model  LIKE '%$yacht_make%') ";
    if(!empty($year_from) && !empty($year_to)){
        $where .= "AND ( year BETWEEN $year_from AND $year_to ) ";
    }
    if(!empty($length_from) && !empty($length_to)){
        $where .= "AND ( length_ft BETWEEN $length_from AND $length_to ) ";
    }
    if(!empty($price_from) && !empty($price_to)){
        $where .= "AND ( price BETWEEN $price_from AND $price_to ) ";
    }
    if ($sail_power != 2){
        $where .= "AND ( sail_power = $sail_power ) ";
    }
    if (count($material_arr) > 0){
        $material = 'AND  (';
        foreach ($material_arr as $value) {
            $material .= ' material LIKE \'%' . $value . '%\' OR';
        }
        $material = substr_replace ( $material , ') ' , -2 );
        $where .= $material;
    }
    if (count($engine_arr) > 0){
        $engine = 'AND  (';
        foreach ($engine_arr as $value) {
            $engine .= ' engine LIKE \'%' . $value . '%\' OR';
        }
        $engine = substr_replace ( $engine , ') ' , -2 );
        $where .= $engine;
    }
    if (count($type_arr) > 0){
        $type = 'AND  (';
        foreach ($type_arr as $value) {
            $type .= ' type LIKE \'' . $value . '\' OR';
        }
        $type = substr_replace ( $type , ') ' , -2 );
        $where .= $type;
    }
    if (count($region_arr) > 0){
        $region = 'AND  (';
        foreach ($region_arr as $value) {
            $region .= ' region LIKE \'' . $value . '\' OR';
        }
        $region = substr_replace ( $region , ') ' , -2 );
        $where .= $region;
    }       
        $where .= 'AND ( active = 1 ) ORDER BY yacht_id DESC';
}

$sql = "SELECT * FROM $tbl_name $where LIMIT $start, $limit";
$result = mysql_query($sql);

最佳答案

有很多方法可以做到这一点,我认为最简单的一种是:

$search = preg_replace('/\s+/','|', $yacht_make);
$sql = "select * from yacht where concat_ws(' ',make,model) rlike '$search'";

这会用 | 替换所有空格,在所有可搜索字段串联的类似正则表达式的查询中用作 OR 。在流量大的网站中,它的速度可能会受到质疑,但它非常紧凑,并且很容易添加更多字段。

关于php - MySql 搜索两个组合字段的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573942/

相关文章:

php - Laravel 5.3 在 ubuntu16.04 nginx 中使用命令composer install --no-dev 上传

sql-server - sql server 全文搜索 - 在全文搜索中查找同义词库的示例用法

mysql - 仅查找与 FULLTEXT 索引完全匹配的内容

php - 404 错误页面的奇怪问题

php - 我遇到过这种语法 : var == "" ? "-": var. 有人可以解释一下吗?

php - 查询为两个唯一字段中的每一个选择具有特定 ID 的行并计算第三个字段的位置

php - 将 X 个数组主菜转换为单个变量

PHP 重新加载时不更新

javascript - 在 MVC 中使用 jquery 突出显示搜索词

java - 如何用Java模拟$.ajax数组POST请求