可能重复:
Split string in SQL
Searching a column with comma seperated valuesSQL查询来从逗号检索值分隔栏
在我PHP项目中,我面临的一个问题写SQL :
我有一个表“consulta NTS“包含字段类别具有类似的值:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ id | consultant_name | categories +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 | AOAOAO | health,insurance,programming, +
+ 2 | BOBOBO | health,gaming,windows,mobile, +
+ 3 | CCCCCC | insurance,windows, +
+ 4 | DDDDDD | mobile, +
+ . | ...... | ............ +
+ . | ...... | ............ +
+ . | ...... | ............ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
而且我有包含所有类别一个顾问可以与注册阵列。
$arrayOfCategories = $array("health","insurance","programming",
"sports","gaming","windows","apple",
"mobile","astrology");
我要的是一个SQL查询应该给我等的输出:
+++++++++++++++++++++++++++++++
+ category | occurrence +
+++++++++++++++++++++++++++++++
+ health | 2 +
+ insurance | 2 +
+ programming | 1 +
+ sports | 0 +
+ gaming | 1 +
+ windows | 2 +
+ apple | 0 +
+ mobile | 2 +
+ astrology | 0 +
+++++++++++++++++++++++++++++++
任何形式的帮助将不胜感激...
在此先感谢.. 。
这就是所谓的不良数据库设计。 – 2012-01-07 13:27:12
@ShaktiSingh:是的,我同意你的意见。但如果最后一次要求更改,该怎么办。 – PhpStudent 2012-01-07 13:28:19
您正在寻找['GROUP_CONCAT()'](http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat)的相反部分。 – hakre 2012-01-07 13:30:43