MySQL-使用LIKE吗?多列搜索

时间:2018-09-18 10:47:13

标签: mysql

我已经查看了Stackoverflow,但似乎找不到我想要的东西。 我有一个动态更新的AJAX搜索表单,其中显示了数据库中的位置数据。

此查询存在我的问题:

 $sql = "SELECT location FROM location_data WHERE location LIKE ? LIMIT 10";

让我解释一下首先发生的事情。数据库表中有3个不同的列,一列称为位置,一列称为CRS,一列称为tiploc。 我想显示如下结果:

Select location FROM location_data WHERE location(textbox) is LIKE ?(what the person typed in) OR CRS is LIKE ? or TIPLOC is LIKE ?

现在,到目前为止,我仅在CRS上进行过尝试,并且我完成了以下查询:

 $sql = "SELECT location FROM location_data WHERE location OR CRS LIKE ? LIMIT 10";

上面仅显示CRS结果(完全匹配),不提供任何位置建议,仅显示CRS。有谁知道我可以如何修改查询,以便它可以同时搜索位置和CRS和TIPLOC,可以搜索位置,但只能完全匹配CRS和TIPLOC?

if(isset($_REQUEST['term'])){
    // Prepare a select statement
    $sql = "SELECT location FROM location_data WHERE location LIKE ? LIMIT 10";

    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);

        // Set parameters
        $param_term = $_REQUEST['term'] . '%';

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);

            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo "<p>" . $row["location"] . "</p>";
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

// close connection
mysqli_close($link);

现在是页面搜索,我从中提取输入的字段称为“位置”。

--Code for JS AJAX Search--

        <script type="text/javascript">
        $(document).ready(function(){
        $('.search-box input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        $(".result").show();
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(inputVal.length >2){
            $.get("backend-search.php", {term: inputVal}).done(function(data){
                // Display the returned data in browser
                resultDropdown.html(data);
          });
        } else{
            resultDropdown.empty();
        }

        });
    // Set search input value on click of result item
        $(document).on("click", ".result p", function(){
        $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
    });
        });
$(document).click(function(){
$(".result").hide();
});
</script>

2 个答案:

答案 0 :(得分:1)

您需要为每列重复function closePillsOnClick(e){ var clickedId = $(this).attr("id"); var clickedIdChild = clickedId.split("-tab").join(""); if( $(this).hasClass("active") ){ $(this).removeClass("active show"); e.stopPropagation(); $("#"+clickedIdChild).removeClass("active show").css("display", "none"); } else { $("#"+clickedIdChild).css("display", "block"); $("#"+clickedIdChild).siblings().css("display", "none"); } }; 表达式。

LIKE

由于查询中现在有3个占位符,因此您需要使用绑定将它们全部填充:

$sql = "SELECT location 
        FROM location_data 
        WHERE location LIKE ? OR CRS LIKE ? OR TIPLOC LIKE ? 
        LIMIT 10";

答案 1 :(得分:0)

对于OR中的每个单个表达式,您必须指定它们的比较条件。请注意,用location LIKE ?代替LOCATION OR

 $sql = "SELECT location 
         FROM location_data 
         WHERE location LIKE ? 
           OR CRS LIKE ? 
           OR TIPLOC LIKE ? 
         LIMIT 10";

注意:不含LIMIT的{​​{1}}子句本质上是不确定的,因为MySQL存储的是无序数据集。基本上,这意味着MySQL可以返回任何10行(如果不使用ORDER BY)。