使用" group by"并且"有"在查询中

时间:2015-09-02 17:45:23

标签: mysql

我喜欢在下面写一些类似的查询,其中列xX_role的行数多于1.我该怎么做。以下是我想要做的类似的事情。我正在尝试使用类似的列值过滤数据。

Select
  scrs.requestid,
  scr_remedy,
  type,
  workstream,
  system,
  status,
  scrs.release,
  assigned_to,
  `role`,
  LEFT(role, 7) as 'xx_role',
  MID(role, 5,3) as 'roleid'
from
  scrs,
  tblscr_role 
GROUP BY 'xx_role'
HAVING
  (scrs.requestid = tblscr_role.requestid)
  and (scrs.release = 'GROUP6' OR scrs.release = 'ER_JUNE15' OR scrs.release = 'ER_APR_15')
  and Count('xx_role') > 1

1 个答案:

答案 0 :(得分:2)

除了@MichaelBerkowski提到的,​​你很可能滥用臭名昭着的MySQL扩展到GROUP BY。读这个。

http://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html

您还有from scrs, tblsc_role的混杂加入。在HAVING条款中加入条件不会按照您的意愿行事。

Select
  scrs.requestid,
  scr_remedy,
  type,
  workstream,
  system,
  status,
  scrs.release,
  assigned_to,
  `role`,
  LEFT(role, 7) as xx_role,
  MID(role, 5,3) as roleid
 from scrs
 join tblscr_role ON scrs.requestid = tblscr_role.requestid
where (    scrs.release = 'GROUP6'
        OR scrs.release = 'ER_JUNE15'
        OR scrs.release = 'ER_APR_15')

从该查询中获得所需的详细结果后,您可以继续添加GROUP BY / HAVING子句。但是尝试使用标准的GROUP BY。