简体   繁体   English

使用colums值作为参数计算所有字符串的出现次数-MySQL

[英]Count occurrences of all strings using the colums value as parameter - mysql

I'm gathering some data information for a project, I would like to see what words repeat more in the database using the column values as search parameters, like: 我正在收集项目的一些数据信息,我想看看使用列值作为搜索参数在数据库中重复哪些单词,例如:

table_product
  id  | name         | description
  1   | blue shirt   | cool shirt 
  2   | yellow pants | pretty nice pants
  3   | red shirt    | cool red shirt

The expected result is something like: 预期的结果是这样的:

field   |  count
shirt   |  4
cool    |  2
pants   |  2
...     |  ...

Is there a way to achieve this using only querys? 有没有一种方法可以仅使用查询来实现? Thanks. 谢谢。

EDIT: I can't pre-set a word list, the goal is to cross all the string and count the occurrences. 编辑:我不能预设一个单词列表,目标是跨越所有字符串并计算出现的次数。

to do this type of result you will have to union each word as a separate query. 要执行这种类型的结果,您必须将每个单词合并为一个单独的查询。 like so.. 像这样..

SELECT 
'shirt' AS field,
SUM(
    CASE WHEN LOCATE('shirt', `name`) > 1 AND LOCATE('shirt', `description`) THEN 2 
         WHEN LOCATE('shirt', `name`) > 1 OR LOCATE('shirt', `description`) THEN 1
         ELSE 0 
    END ) AS 'Count'
FROM table_product

UNION

SELECT 
'cool' AS field,
SUM(
    CASE WHEN LOCATE('cool', `name`) > 1 AND LOCATE('cool', `description`) THEN 2 
         WHEN LOCATE('cool', `name`) > 1 OR LOCATE('cool', `description`) THEN 1
         ELSE 0 
    END ) AS 'Count'
FROM table_product

UNION

SELECT 
'pants' AS field,
SUM(
    CASE WHEN LOCATE('pants', `name`) > 1 AND LOCATE('pants', `description`) THEN 2 
         WHEN LOCATE('pants', `name`) > 1 OR LOCATE('pants', `description`) THEN 1
         ELSE 0 
    END ) AS 'Count'
FROM table_product

DEMO DEMO

RECOMMENDATION: 建议:

I would recommend you just make a new column for each count you want.. so in other words just use a pivoted result.. as it'll be much easier to handle and if you are trying to get the result in another programming language the key will be the name aka 'shirt', 'cool'... and the value will be the count.. demonstration 我建议您只为想要的每个计数添加一个新列..换句话说,只需使用枢轴结果..因为它会更容易处理,并且如果您尝试使用另一种编程语言来获得结果,键将被称为“衬衫”,“酷”的名称,而值将是计数。

SELECT 
    SUM(
    CASE WHEN LOCATE('shirt', `name`) > 1 AND LOCATE('shirt', `description`) THEN 2 
         WHEN LOCATE('shirt', `name`) > 1 OR LOCATE('shirt', `description`) THEN 1
         ELSE 0 
    END ) AS 'shirt',

    SUM(
    CASE WHEN LOCATE('cool', `name`) > 1 AND LOCATE('cool', `description`) THEN 2 
         WHEN LOCATE('cool', `name`) > 1 OR LOCATE('cool', `description`) THEN 1
         ELSE 0 
    END ) AS 'cool',

    SUM(
    CASE WHEN LOCATE('pants', `name`) > 1 AND LOCATE('pants', `description`) THEN 2 
         WHEN LOCATE('pants', `name`) > 1 OR LOCATE('pants', `description`) THEN 1
         ELSE 0 
    END ) AS 'pants'
FROM table_product;

Thanks John for the information and your time. 感谢约翰提供的信息和您的时间。

For whom it may be usefull, I developed a quick script to do this trick in php. 对于谁可能有用,我开发了一个快速脚本来在php中完成此技巧。

PS: This is by no means the best way you can do it, but it'll work. PS:这绝不是您可以做到的最好方法,但是它将起作用。

//search the strings
$result = mysql_query("SELECT field_a, field_b FROM table");

$arrayMaster = array();
while ($row = mysql_fetch_array($result)) {
    //explode the spaces to get each word
    $field_a = explode(' ', $row['field_a']);
    foreach ($field_a as $string) {
        $string = strtolower($string);
        //remove words with less then 4 charac
        if(strlen($string) < 4){
            continue;
        }
        //check if there is a position on the array with that string, if it exists ++
        if( isset($arrayMaster[$string]) ){         
            $arrayMaster[$string]++;
        } else {
            $arrayMaster[$string] = 1;
        }
    }

    $field_b = explode(' ', $row['field_b']);
    foreach ($field_b as $string) {
        $string = strtolower($string);
        if(strlen($string) < 4){
            continue;
        }
        if( isset($arrayMaster[$string]) ){         
            $arrayMaster[$string]++;
        } else {
            $arrayMaster[$string] = 1;
        }
    }
}
//sort the array in reverse order
arsort($arrayMaster);
//print the words and the amount that they show up
print_r($arrayMaster);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM