MySQL查询中的多个LIKE比较? - MySQL、PHP

发布于 2024-12-28 18:32:45 字数 1812 浏览 0 评论 0原文

我有一个搜索表单字段,我想将其与表中的多个列进行比较,例如:

+----------+-------+---------+------+------------+
| name     | owner | species | sex  | birth      |
+----------+-------+---------+------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 |
| Bowser   | Diane | dog     | m    | 1989-08-31 |
| Whistler | Gwen  | cat     | m    | 1997-12-09 |
+----------+-------+---------+------+------------+

我所做的查询有多个 LIKE 比较,但它似乎只找到前两列的数据行。

第一个比较是“名称”,然后是“所有者”,然后是“物种”,然后是“性别”。

如果您输入与前两列匹配的字符串,它只会查找行,但是如果您输入与最后一列匹配的字符串,它不会返回任何内容。

这是查询:

SELECT * FROM contact 
WHERE name LIKE \"%$search%\" 
OR owner LIKE \"%$search%\"
OR species LIKE \"%$search%\" 
OR sex LIKE \"%$search%\" 
GROUP BY name, owner,species, sex

我唯一的猜测是 MySQL 可能限制了您可以使用多少个“OR”?

如果用户输入的字符串与任何列数据匹配,我的查询应该从表中返回一行。

这是我的 PHP 代码,

include("database.class.php");

    //search string assigned from POST field
    $search = $_POST['contact_search'];

    $db = new database();

    $db->select('quote_system');

    $result = $db->query("SELECT * FROM contact WHERE name LIKE \"%$search%\" OR owner LIKE \"%$search%\" OR species LIKE \"%$search%\" OR sex LIKE \"%$search%\" GROUP BY name, second_name, owner, species, sex");

    print_r($result);

    while ($row = $db->fetch_assoc($result))
    {
        echo $row['name'];
        echo $row['owner'];
        echo $row['species'];
        echo $row['sex'];
        echo "<br/>";

    }

以防出现任何混淆,这里有一些示例来说明问题:

用户输入字符串“claws”-->字符串匹配“名称”列中的数据 -->打印该列所在的行

用户输入字符串“m”-->字符串仅匹配“性别”列中的数据 -->什么也没显示!

感谢您抽出时间!

I have a search form field that I want to compare with multiple columns within a table e.g.:

+----------+-------+---------+------+------------+
| name     | owner | species | sex  | birth      |
+----------+-------+---------+------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 |
| Bowser   | Diane | dog     | m    | 1989-08-31 |
| Whistler | Gwen  | cat     | m    | 1997-12-09 |
+----------+-------+---------+------+------------+

The query I made has multiple LIKE comparisons however it only seems to find rows of data for the first two columns.

The first comparison is "name" then "owner" then "species" then "sex".

It only finds rows if you enter a string that matches the first two columns, however if you enter a string that matches one of the last columns it does not return anything.

Here is the query:

SELECT * FROM contact 
WHERE name LIKE \"%$search%\" 
OR owner LIKE \"%$search%\"
OR species LIKE \"%$search%\" 
OR sex LIKE \"%$search%\" 
GROUP BY name, owner,species, sex

My only guess is that MySQL perhaps limits how many "OR"s you can use?

My query should return a row from the table if the string entered by the user matches any column data.

Here is my PHP code for it

include("database.class.php");

    //search string assigned from POST field
    $search = $_POST['contact_search'];

    $db = new database();

    $db->select('quote_system');

    $result = $db->query("SELECT * FROM contact WHERE name LIKE \"%$search%\" OR owner LIKE \"%$search%\" OR species LIKE \"%$search%\" OR sex LIKE \"%$search%\" GROUP BY name, second_name, owner, species, sex");

    print_r($result);

    while ($row = $db->fetch_assoc($result))
    {
        echo $row['name'];
        echo $row['owner'];
        echo $row['species'];
        echo $row['sex'];
        echo "<br/>";

    }

Incase theres any confusion here are some examples to show the problem:

User enters string "claws" --> string matches data in "name" column --> prints row that column is in

User enters string "m" --> string only matches data in "sex" column --> nothing shows!

Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2025-01-04 18:32:45

您的查询

$result = $db->query("
  SELECT * FROM contact 
  WHERE first_name LIKE \"%$search%\" 
  OR second_name LIKE \"%$search%\" 
  OR company_name LIKE \"%$search%\" 
  OR email LIKE \"%$search%\" 
  GROUP BY first_name, second_name, company_name, email
");

似乎属于另一个表。另外,为什么要进行 GROUP BY?也许,你的意思是 ORDER BY?

Your query

$result = $db->query("
  SELECT * FROM contact 
  WHERE first_name LIKE \"%$search%\" 
  OR second_name LIKE \"%$search%\" 
  OR company_name LIKE \"%$search%\" 
  OR email LIKE \"%$search%\" 
  GROUP BY first_name, second_name, company_name, email
");

seems to belong to another table. Also, why do you GROUP BY? Maybe, you meant ORDER BY?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文