SQL 查询从逗号分隔列中检索值
可能的重复:
在 SQL 中分割字符串
搜索包含逗号分隔值的列
在我的PHP< /strong> 项目 我在编写 SQL 时遇到问题:
我有一个表“consultants”,其中包含字段categories,其值如下:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 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 +
+++++++++++++++++++++++++++++++
任何类型的帮助将不胜感激...
提前致谢...
Possible Duplicate:
Split string in SQL
Searching a column with comma seperated values
In my PHP project I'm facing a problem to write a SQL:
I have a table "consultants" with fields categories which has values like:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ id | consultant_name | categories +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 | AOAOAO | health,insurance,programming, +
+ 2 | BOBOBO | health,gaming,windows,mobile, +
+ 3 | CCCCCC | insurance,windows, +
+ 4 | DDDDDD | mobile, +
+ . | ...... | ............ +
+ . | ...... | ............ +
+ . | ...... | ............ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
And I have an array which contains all the categories that a consultant can register with.
$arrayOfCategories = $array("health","insurance","programming",
"sports","gaming","windows","apple",
"mobile","astrology");
What I want is a SQL Query that should give me an output like:
+++++++++++++++++++++++++++++++
+ category | occurrence +
+++++++++++++++++++++++++++++++
+ health | 2 +
+ insurance | 2 +
+ programming | 1 +
+ sports | 0 +
+ gaming | 1 +
+ windows | 2 +
+ apple | 0 +
+ mobile | 2 +
+ astrology | 0 +
+++++++++++++++++++++++++++++++
Any kind of help will be appreciated...
Thanks In Advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
糟糕的数据库设计但有趣的问题,这是我的做法(php代码):
生成的查询的结果应该给你你想要的。糟糕的设计常常会带来有趣的问题......:)
Bad database design but interesting question, here it is my way to do (php code):
The result of the generated query should give you exactly what you want . Often from a bad design comes interesting questions ... :)
这是一个糟糕的数据库设计。如果我坚持下去,我不会尝试使用 SQL 来获取事件,而是使用我的应用程序。
创建一个名为 $occurrence 的新数组,其中包含每个单词的索引和计数。
选择每一行并用逗号分割类别的内容。循环遍历这个结果数组并为每个单词递增。
我对 PHP 有点生疏,所以这是我对 Perl 的建议:
这种方式的好处之一是您会发现任何拼写错误的类别,并且在添加新类别时不必更新 SQL。
This is a bad database design. If I were stuck with it, I wouldn't try to use SQL to get the occurrences, but my application.
Create a new array called $occurence, this will have an index of each word and a count.
Select every row and split the contents of categories on the comma. Loop through this resulting array and increment for every word.
I'm a little rusty in PHP so here is my suggestion in Perl:
One of the benefits of this way is you will find any mispelled categories and don't have to update your SQL when a new category gets added.
如果您有一个包含类别的表,您可以这样做:
尽管如此,我认为这很尴尬,您应该考虑规范化数据库方案,以便有一个顾问类别关系的映射表
If you had a table with categories, you could do it like this:
Still, I consider this awkward and you should consider normalising the db scheme so that there's a mapping table for the consultant-category relation