欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-22 10:04:39
...

如何通过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中文网

相关标签: php mysql