SQL 查询从逗号分隔列中检索值

发布于 2024-12-25 13:24:25 字数 1978 浏览 1 评论 0原文

可能的重复:
在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从来不烧饼 2025-01-01 13:24:25

糟糕的数据库设计但有趣的问题,这是我的做法(php代码):

$arrayOfCategories =  $array("health","insurance","programming",
                             "sports","gaming","windows","apple",
                             "mobile","astrology");

 $count = count($arrayOfCategories);
 $query = "";
 for($i = 0; $i < $count; $i++)
 {
   $value = $arrayOfCategories[$i];
   $query += "SELECT '$value' AS category,
                 COUNT(*) AS occurrence
                 FROM consultants
                 WHERE FIND_IN_SET('$value', categories)";
    if($i < $count -1) {
    $query += " UNION "
    }


 }

生成的查询的结果应该给你你想要的。糟糕的设计常常会带来有趣的问题......:)

Bad database design but interesting question, here it is my way to do (php code):

$arrayOfCategories =  $array("health","insurance","programming",
                             "sports","gaming","windows","apple",
                             "mobile","astrology");

 $count = count($arrayOfCategories);
 $query = "";
 for($i = 0; $i < $count; $i++)
 {
   $value = $arrayOfCategories[$i];
   $query += "SELECT '$value' AS category,
                 COUNT(*) AS occurrence
                 FROM consultants
                 WHERE FIND_IN_SET('$value', categories)";
    if($i < $count -1) {
    $query += " UNION "
    }


 }

The result of the generated query should give you exactly what you want . Often from a bad design comes interesting questions ... :)

柏拉图鍀咏恒 2025-01-01 13:24:25

这是一个糟糕的数据库设计。如果我坚持下去,我不会尝试使用 SQL 来获取事件,而是使用我的应用程序。

创建一个名为 $occurrence 的新数组,其中包含每个单词的索引和计数。
选择每一行并用逗号分割类别的内容。循环遍历这个结果数组并为每个单词递增。

我对 PHP 有点生疏,所以这是我对 Perl 的建议:

my %occurrence;

while ( my ($categories) = $sth->fetchrow_array() ){

   my @cats = split(',', $categories);

   foreach my $c (@cats){
      $occurrence{$c}++;
   }

}

这种方式的好处之一是您会发现任何拼写错误的类别,并且在添加新类别时不必更新 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:

my %occurrence;

while ( my ($categories) = $sth->fetchrow_array() ){

   my @cats = split(',', $categories);

   foreach my $c (@cats){
      $occurrence{$c}++;
   }

}

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.

佞臣 2025-01-01 13:24:25

如果您有一个包含类别的表,您可以这样做:

SELECT cat.name, COUNT(*) 
FROM categories cat JOIN consultants con ON con.categories LIKE '%'+cat.name+'%'

尽管如此,我认为这很尴尬,您应该考虑规范化数据库方案,以便有一个顾问类别关系的映射表

If you had a table with categories, you could do it like this:

SELECT cat.name, COUNT(*) 
FROM categories cat JOIN consultants con ON con.categories LIKE '%'+cat.name+'%'

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文