Mysql通过通配符选择查询分组

时间:2012-04-30 16:23:55

标签: mysql select sum wildcard

无法通过通配符对活动论坛和结果进行分组。

最终,我希望所有具有相似名称的结果分组并汇总在不同的行中。

table:forums

列:游戏,帖子

当前查询:

SELECT game, sum(posts) FROM forums WHERE posts>1 AND game like
'%rift%' OR game like '%swiftsure%' GROUP BY name

目前的结果:

  

游戏帖子

     

Rift(PVP Defiant):Briarcliff 3406

     

裂痕指南 100

     

裂痕领导 1541

     

裂谷突袭讨论 1336

     

Rift Raid领导 533

     

裂痕:牧师 85

     裂痕:法师 87

     

裂痕:私人 411

     

裂痕:PVE领导 113

     

裂痕:流氓 56

     

裂痕:战士 29

     

星球大战旧共和国PvP - Swiftsure 7

     

SWTOR Swiftsure Leadership 0

     

SWTOR:Swiftsure Private 7

期望的结果如下所示:

  

游戏|的帖子

     

裂痕|的 7697

     

Swiftsure |的 14

我当然感谢所有人的帮助!

4 个答案:

答案 0 :(得分:2)

试试这个:http://www.sqlfiddle.com/#!2/f6be7/4

select x.Name, COUNT(p.Name)
from
(
  SELECT 'JOHN' AS NAME
  UNION
  SELECT 'PAUL'  
  ) as x
LEFT JOIN Person p ON p.Name LIKE CONCAT('%',x.NAME, '%')
GROUP BY x.Name

示例数据:

create table person
(
  name varchar(100)
  );


insert into person values
('JOHN LENNON'),
('WINSTON JOHN LENNON'),
('MICHAEL JOHN FOX'),
('PAUL McCartney'),
('SEAN PAUL DATA'),
('GREAT PAUL OF FIRE'),
('ST PAUL OF X');

输出:

NAME    COUNT(P.NAME)
JOHN    3
PAUL    4

如果你想算GEORGE,你只需要在UNION上添加它:

select x.Name, COUNT(p.Name)
from
(
  SELECT 'JOHN' AS NAME
  UNION
  SELECT 'PAUL'  
  UNION
  SELECT 'GEORGE'
  ) as x
LEFT JOIN Person p ON p.Name LIKE CONCAT('%',x.NAME, '%')
GROUP BY x.Name

输出:

NAME    COUNT(P.NAME)
GEORGE  0
JOHN    3
PAUL    4

太糟糕MySQL没有不必要的功能。使用Postgresql更简单:http://www.sqlfiddle.com/#!1/87c74/5

select x.Name, count(p.Name)
from 
unnest(array['John','Paul','George']) as x(Name)
left join Person p ON p.Name ilike concat('%',x.Name, '%')
group by x.Name

您甚至可以直接使用从服务器端语言到Postgresql的逗号分隔字符串,只需在逗号分隔的字符串周围加上一个大括号:http://www.sqlfiddle.com/#!1/87c74/17

select x.Name, count(p.Name)
from 
unnest('{John,Paul,George}'::text[]) as x(Name)
left join Person p ON p.Name ilike concat('%',x.Name, '%')
group by x.Name

这也适用于Postgresql,值列表可以直接用于查询(而在MySQL中,值列表只能在DDL上使用):http://www.sqlfiddle.com/#!1/87c74/9

select x.Name, count(p.Name)
from 
(values('John'),('Paul'),('George')) as x(Name)
left join Person p ON p.Name ilike concat('%',x.Name, '%')
group by x.Name

FROM子句上的值列表也适用于Microsoft Sql Server:http://www.sqlfiddle.com/#!3/87c74/3

答案 1 :(得分:1)

由于您正在寻找通用分组,我会做一个CASE / WHEN作为列......

SELECT case when game like '%rift%'      then 'Rift   '
            when game like '%swiftsure%' then 'Swiftsure'
       end as GroupName,
       count(*) as  PostCount
   from
      forums
   where
         game like '%rift%' 
      OR game like '%swiftsure%' 
   GROUP BY
      GroupName

答案 2 :(得分:0)

我可能会选择:

  INSERT INTO working_table (keyword, connection)
  VALUES ('rift', connection_id())('swiftsure', connection_id());

  SELECT w.keyword, count(*)
  FROM forums f
  INNER JOIN working_table w
  ON f.game LIKE CONCAT('%', w.keyword, '%')
  WHERE w.connection=connection_id()
  AND forms.posts>1
  GROUP BY w.keyword;

  DELETE FROM working_table
  WHERE connection=connection_id();

虽然它会像一条2条腿的狗一样跑。真的,如果我这样做,我会使用正确的搜索引擎架构。

答案 3 :(得分:0)

您需要将两个OR条件放在括号中。并且在没有它们的情况下具有更高的优先级。

SELECT case when game like ... end as game, sum(posts)
FROM forums
WHERE posts > 1 AND (game like '%rift%' OR game like '%swiftsure%')
GROUP BY name