简体   繁体   English

如何在一个表中搜索多行?

[英]How do I search a table for multiple rows?

So this is my first question on stackoverflow and I've searched for day's but can't find the results I am looking for. 所以这是我关于stackoverflow的第一个问题,我已经搜索了一天,但是找不到我想要的结果。 I'm not looking for someone to write the code for me as I prefer to figure it out on my own, but I am looking for some assistance on finding how to do it. 我不是在找人为我编写代码,因为我更喜欢自己弄清楚代码,但是我正在寻找一些帮助,以寻求如何做的代码。

My problem is I don't know how to word my search for what I'm looking for and this is what I want to do. 我的问题是我不知道该如何用词表达我要寻找的东西,而这正是我想要做的。 I want to be able to search a MySQL database by using keywords such as "U10 Mustangs" where U10 would be data in a row called 'divisions' and Mustangs would be data in a row called 'club', I know I can use the OR statement like this 我希望能够通过使用诸如“ U10野马”之类的关键字搜索MySQL数据库,其中U10将是连续数据中的“部门”,而野马则是连续数据中的“俱乐部”,我知道我可以使用或这样的陈述

(`division` LIKE '%".$query."%') OR (`club` LIKE '%".$query."%')

but that only allows me to search by division or club not both. 但这仅允许我按部门或俱乐部进行搜索,而不能同时使用两者进行搜索。 So basically I'm looking for a tutorial to show me how I can search by more than one keyword. 因此,基本上,我正在寻找一个教程,向我展示如何通过多个关键字进行搜索。 I'm sure I'm wording this wrong and that's why I'm unable to find what I want. 我确定我说错了这,这就是为什么我找不到想要的东西。

I'm new to MySQL and PHP so please be understanding if this makes no sense! 我是MySQL和PHP的新手,所以请理解这是否没有道理!

* UPDATE * here is my search code: * 更新 *这是我的搜索代码:

    <body>
<?php include 'menu.php';?>
<br/><br/><br/>
<div id="searchcontainer" style="width: 55%; height: 132px;">
<fieldset style="width: 330px"><legend>Search Criteria</legend>
<form action="search.php" method="POST">
<td>Enter Search</td><td>&nbsp; <input type="text" name="query"/></td><td>  <input type="submit" name="submit" id="table_button" value="Search" action="search.php"/></td>
<br />
</form>
</fieldset><br/>
<fieldset><legend>Search Results</legend>
<?php
        include 'connect/local_connect.php';

    $query = $_POST['query']; 
    // gets value sent over search form

   // $min_length = 3;
   // you can set minimum length of the query if you want

   // if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM players
            WHERE (`team_name` LIKE '%".$query."%' AND `division` LIKE '%".$query."%') OR (`last_name` LIKE '%".$query."%') OR (`division` LIKE '%".$query."%') ORDER by id ASC") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `first_name`, `any`
        // players is the name of our table



        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

        }
        else{ // if there is no matching rows do following
            echo "<font color=red>No Results!</font>";
      ?><br/><br/>
      <?php

    }
   // else{ // if query length is less than minimum
   //     echo "Minimum length is ".$min_length;
   // }


        // Define $color=1 
        $color="1";
        echo '<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">';
        echo '<th>ID</th><th>Club</th><th>Last Name</th><th>First Name</th><th>Division</th>';
        while($rows = mysql_fetch_array($raw_results)){

        echo "<tr bgcolor='#fff000'>
        <td><center>".$rows['id']."</td></center><td><center>".$rows['club']."</center></td><td><center>".$rows['last_name']."</td></center><td><center>".$rows['first_name']."</center></td></td><td><center>".$rows['division']."</center></td></td></tr>";       
        }




        echo '</table>';
        mysql_close();
        ?>

</fieldset>
</div>
</body>

于在寻找连任吗

where CONCAT(`division`,' ',`club`) like '%".$query."%'

I will suggest to create a full text index on division and club like this FULLTEXT (club,division) and search using query like this 我建议像这样的FULLTEXT (club,division)在部门和俱乐部上创建全文索引FULLTEXT (club,division)并使用类似这样的查询进行搜索

SELECT * FROM db
WHERE MATCH (club,division)
AGAINST ('search');

'...but that only allows me to search by division or club not both...' '...但是那只允许我按部门或俱乐部进行搜索, 而不能同时搜索...'

You can split your search string in words and then use a condition like this 您可以将搜索字符串分成单词,然后使用类似这样的条件

SELECT * 
  FROM table1
 WHERE (division LIKE '%U10%' AND club LIKE '%Mustang%')
    OR (division LIKE '%Mustang%' AND club LIKE '%U10%')

Here is SQLFiddle demo 这是SQLFiddle演示

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

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