mysql按空格拆分搜索字符串并推入查询 [英] mysql split search string by space and push into query

查看:40
本文介绍了mysql按空格拆分搜索字符串并推入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索查询是:

"product1 prod2 prod4"

"product1 prod2 prod4"

我需要制作 mysql

I need to make the mysql

SELECT * 
  FROM tableprod 
  WHERE (prod LIKE '%product1%' 
         AND prod LIKE '%prod2%' 
         AND prod LIKE '%prod4%')

使用 mysql_real_escape_string 作为输入查询...

using the mysql_real_escape_string for the input query...

推荐答案

简单的字符串操作:

$terms = explode(' ', $search);

$bits = array();
foreach ($terms as $term) {
    $bits[] = "prod LIKE '%".mysql_real_escape_string($term)."%'";
}

$query = "SELECT * FROM tableprod WHERE (".implode(' AND ', $bits).")";

这篇关于mysql按空格拆分搜索字符串并推入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆