MySQL查询中的多个LIKE比较? - MySQL、PHP
我有一个搜索表单字段,我想将其与表中的多个列进行比较,例如:
+----------+-------+---------+------+------------+
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询
似乎属于另一个表。另外,为什么要进行 GROUP BY?也许,你的意思是 ORDER BY?
Your query
seems to belong to another table. Also, why do you GROUP BY? Maybe, you meant ORDER BY?