0
$keywords=array("test","tset");
$matches = implode(',', $keywords);
$sql = "SELECT * FROM `reg` WHERE title like in '%$matches%' group by p_title";

$data = mysql_query($sql); 
while($info=mysql_fetch_array($data))
{
Print " {$info['title']}<br /> ";
}

我想写一个查询

   $sql = "SELECT * FROM `reg` WHERE title like in '%test%' group by p_title";
   $sql = "SELECT * FROM `reg` WHERE title like in '%tset%' group by p_title";

...........

如何在一个查询中搜索每个关键字?

4

5 回答 5

1
"SELECT * FROM reg WHERE title like in '%test%' or  title like in '%tset%' group by p_title"
于 2013-05-08T14:10:20.213 回答
0

看到这篇文章:MySQL LIKE IN()?

$keywords=array("test","tset");
$matches = implode('|', $keywords);
$sql = "SELECT * FROM `reg` WHERE title REGEXP '%$matches%' group by p_title";

$data = mysql_query($sql); 
while($info=mysql_fetch_array($data))
{
    Print " {$info['title']}<br /> ";
}
于 2013-05-08T14:14:09.467 回答
0
SELECT * FROM reg WHERE title like in '%{$keywords[0]}%' or title like in '%{$keywords[1]}%'

我建议为此使用循环。例子:

注意:这是相当假的

$query = "SELECT * FROM reg WHERE title like in ";
foreach ($keyword in $keywords) {
  if (!$keyword is last)
    $query .= "'%$keyword%' or title like in ";
  else $query .= "'%$keyword%' group by p_title"
}

然后评估$query

于 2013-05-08T14:12:34.693 回答
0
$sql = "SELECT * FROM `reg` WHERE title ";

$where='';
foreach($keywords as $val)
{
  $where .= "LIKE '%$val%' OR title ";

}
$where = substr($where , 0, -9); // to remove last OR 
$finalSql = $sql .$where;

$data = mysql_query($finalSql); 
于 2013-05-08T14:18:28.853 回答
0

implodePHP 函数允许在没有循环的情况下实现它:

$keywords=array("test","tset");
$matches = implode("%' OR title LIKE '%", $keywords);
$sql = "SELECT * FROM `reg` WHERE title LIKE '%$matches%' group by p_title";

$data = mysql_query($sql); 
while($info=mysql_fetch_array($data))
{
Print " {$info['title']}<br /> ";
}
于 2013-05-08T14:33:09.847 回答