MySQL 使用GROUP

字体大小: 中小 标准 ->行高大小: 标准

要写一个sql,统计某写游戏玩家级别最高的角色名,首先看一个某个玩家的所有角色

 

  1. linuxidc@www.linuxidc.com  
  2. select a.account,a.`name`,a.`LEVELfrom mdb.char_info a  
  3. inner join test.pushspread b on b.account = a.account  
  4. GROUP BY a.account,a.`name`  

结果,因为使用的是生产环境数据,所以改变了连接的字符集,造成乱码还请大家理解

level 34行数据就是目标数据

灵机一动写了个sql,结果却为空:

 

  1. linuxidc@www.linuxidc.com  
  2. select a.account,a.`name`,a.`LEVEL`,MAX(a.`LEVEL`) t from mdb.char_info a  
  3. inner join test.pushspread b on b.account = a.account  
  4. GROUP BY a.account  
  5. HAVING a.`LEVEL`= t  

没辙还是请出GROUP_CONCAT函数吧 

 

  1. linuxidc@www.linuxidc.com  
  2. SELECT a.account,SUBSTRING_INDEX(GROUP_CONCAT(a.name ORDER BY a.level DESC),',',1) AS name,MAX(a.level)   
  3. FROM mdb.char_info a   
  4. INNER JOIN test.pushspread b ON b.account=a.account   
  5. GROUP BY a.account;  

OK,得到结果

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/57476.html