我正在尝试在 mysql 中搜索数据。到目前为止,如果我知道我要查找的确切名称,则使用 LIKE 可以搜索组名称。 我需要能够使用组名称的一部分进行搜索,并让它显示其中包含某些搜索字符串的所有行。下面是我到目前为止的代码。
$group = $_GET['query_term'];
if (!$link = mysql_connect('', '', '')) {
exit;
}
if (!mysql_select_db('dbname', $link)) {
exit;
}
$sql = "SELECT os_groups_groups.*, useraccounts.* FROM os_groups_groups JOIN useraccounts WHERE os_groups_groups.FounderID = useraccounts.PrincipalID And os_groups_groups.Name LIKE '".$group."'";
最佳答案
将 $sql
更改为:
$sql = "SELECT os_groups_groups.*, useraccounts.* FROM os_groups_groups JOIN useraccounts WHERE os_groups_groups.FounderID = useraccounts.PrincipalID And os_groups_groups.Name LIKE '%".$group."%'";
使用LIKE
,您可以在模式中使用以下两个通配符:
%
匹配任意数量的字符,甚至零个字符_
精确匹配一个字符
您可以在以下位置阅读有关通配符的更多信息:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
关于php mysql 在连接表上搜索部分匹配的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29224569/