Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何按每个单独的单词分组,并在另一列中为该单词组求和值?_Mysql_Sql - Fatal编程技术网

Mysql 如何按每个单独的单词分组,并在另一列中为该单词组求和值?

Mysql 如何按每个单独的单词分组,并在另一列中为该单词组求和值?,mysql,sql,Mysql,Sql,假设我有一个包含如下数据的表: keyword times_phrase_searched ------- --------------------- open windows 1000 closed windows 750 open doors 350 closed doors 250 nice window 100 nice windows 50 ugly doors 25 我需要什么样的SQ

假设我有一个包含如下数据的表:

keyword          times_phrase_searched
-------          ---------------------
open windows     1000
closed windows   750
open doors       350
closed doors     250
nice window      100
nice windows     50
ugly doors       25
我需要什么样的SQL查询才能分别对每个单词进行分组,并对该单词所在短语的搜索量求和。因此,对于示例数据,预期结果为:

word             times_word_in_searches
----             ----------------------
windows          1800
open             1350
closed           1000
doors            625
nice             150
window           100
ugly             25
)


我不确定,但请检查此…

如果您没有数字表,请创建一个(请参见如何创建)。然后可以使用此查询获取字数:

select SUBSTRING_INDEX(SUBSTRING_INDEX(words.keyword, ' ', numbers.n), ' ', -1) word, 
       SUM(words.times_phrase_searched) times_word_in_searches
from numbers 
inner join words on CHAR_LENGTH(words.keyword) - CHAR_LENGTH(REPLACE(words.keyword, ' ', '')) >= numbers.n - 1
group by word
order by sum(words.times_phrase_searched) desc;
这将拆分关键字列,以便在其自己的行中获得每个单独的关键字。然后对
进行分组
求和
就很简单了

试试这个

DECLARE @temp TABLE 
  ( 
     keyword VARCHAR(max), 
     times   INT 
  ) 

INSERT INTO @temp 
VALUES      ('open windows', 
             1000 ), 
            ('closed windows', 
             750 ), 
            ('open doors', 
             350 ), 
            ('closed doors', 
             250 ), 
            ('nice window', 
             100 ), 
            ('nice windows', 
             50 ), 
            ('ugly doors', 
             25 ) 

DECLARE @allValues VARCHAR(max) = (SELECT Stuff((SELECT 
                                          ',' + Replace(p2.keyword, ' ', 
                                          ',') 
                 FROM   @temp p2 
                 ORDER  BY p2.keyword 
                 FOR xml path(''), type).value('.', 'varchar(max)'), 1, 1, '')) 


-- find distinct words 
SELECT DISTINCT t.element, 
                (SELECT Sum(k.times) 
                 FROM   @temp k 
                 WHERE  k.keyword LIKE '%' + t.element + '%') 
FROM   dbo.Func_split(@allValues, ',') t 
函数
Func\u split
(学分:)


这不是MySQL。
DECLARE @temp TABLE 
  ( 
     keyword VARCHAR(max), 
     times   INT 
  ) 

INSERT INTO @temp 
VALUES      ('open windows', 
             1000 ), 
            ('closed windows', 
             750 ), 
            ('open doors', 
             350 ), 
            ('closed doors', 
             250 ), 
            ('nice window', 
             100 ), 
            ('nice windows', 
             50 ), 
            ('ugly doors', 
             25 ) 

DECLARE @allValues VARCHAR(max) = (SELECT Stuff((SELECT 
                                          ',' + Replace(p2.keyword, ' ', 
                                          ',') 
                 FROM   @temp p2 
                 ORDER  BY p2.keyword 
                 FOR xml path(''), type).value('.', 'varchar(max)'), 1, 1, '')) 


-- find distinct words 
SELECT DISTINCT t.element, 
                (SELECT Sum(k.times) 
                 FROM   @temp k 
                 WHERE  k.keyword LIKE '%' + t.element + '%') 
FROM   dbo.Func_split(@allValues, ',') t 
CREATE FUNCTION [dbo].[func_Split] 
    (   
    @DelimitedString    varchar(8000),
    @Delimiter              varchar(100) 
    )
RETURNS @tblArray TABLE
    (
    ElementID   int IDENTITY(1,1),  -- Array index
    Element     varchar(1000)               -- Array element contents
    )
AS
BEGIN

    -- Local Variable Declarations
    -- ---------------------------
    DECLARE @Index      smallint,
                    @Start      smallint,
                    @DelSize    smallint

    SET @DelSize = LEN(@Delimiter)

    -- Loop through source string and add elements to destination table array
    -- ----------------------------------------------------------------------
    WHILE LEN(@DelimitedString) > 0
    BEGIN

        SET @Index = CHARINDEX(@Delimiter, @DelimitedString)

        IF @Index = 0
            BEGIN

                INSERT INTO
                    @tblArray 
                    (Element)
                VALUES
                    (LTRIM(RTRIM(@DelimitedString)))

                BREAK
            END
        ELSE
            BEGIN

                INSERT INTO
                    @tblArray 
                    (Element)
                VALUES
                    (LTRIM(RTRIM(SUBSTRING(@DelimitedString, 1,@Index - 1))))

                SET @Start = @Index + @DelSize
                SET @DelimitedString = SUBSTRING(@DelimitedString, @Start , LEN(@DelimitedString) - @Start + 1)

            END
    END

    RETURN
END