Home  >  Article  >  Backend Development  >  如何通过mysql一次搜索出所有不重复的tag

如何通过mysql一次搜索出所有不重复的tag

PHPz
PHPzOriginal
2016-06-06 20:52:031312browse

如何通过mysql一次搜索出所有不重复的tag

具体问题:

MYSQL精确搜索和PHP数组操作的问题

mysql中的TAG字段包含如下情况

php
php,mysql
jquery
html
php
ruby
java,jquery,js
java
html
css

我希望能通过mysql一次搜索出所有不重复的tag,就像这样的结果

php
mysql
jquery
html
ruby
java
css

如果一次搜索不出来的话通过尽可能简单的PHP数组操作一下也行,请高手指点

方法:

 

额,一个SQL操作成功貌似对我难度有点大,我的想法是:

先老老实实的读取

code:
    SELECT * FROM tag
result example:
    $result = array('php','php,mysql','jquery','html','php','ruby','java,jquery,js','java','html','css');

利用implode函数连接数组变成字符串(连接用的字串为,)

code:
    $result = implode(',',$result);
result example:
    $result = 'php,php,mysql,jquery,html,php,ruby,java,jquery,js,java,html,css';

利用explode函数剪断字符串重新变成数组(剪断用的字串为,)

code:
    $result = explode(',',$result);
result example:
    $result = Array ( [0] => php [1] => php [2] => mysql [3] => jquery [4] => html [5] => php [6] => ruby [7] => java [8] => jquery [9] => js [10] => java [11] => html [12] => css );

最后利用array_unique 函数去除重复值即可

code:
    $result = array_unique($result);
result example:
    $result = Array ( [0] => php [2] => mysql [3] => jquery [4] => html [6] => ruby [7] => java [9] => js [12] => css )

更多相关技术文章,请访问PHP中文网

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn