展开GROUP BY和HAVING结果集 [英] Expand a GROUP BY and HAVING resultset

查看:91
本文介绍了展开GROUP BY和HAVING结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法展开/获取 GROUP BY HAVING 查询的所有记录?



例如,

  SELECT Column1,Column2,Column3,Count(* )as Count FROM table_name 
GROUP BY Column1,Column2,Column3
HAVING Count> '2'

是否有更简单的方法来获取所有记录而不是通过结果集和对于每条记录,执行 SELECT * FROM table_name WHERE Column1 ='this'AND Column2 ='that'AND Column3 ='more'

如果由于mysql或其他限制而无法完成,是否有其他方法可以获取 query 以上的所有数据?



通过展开/获取所有记录,我的意思是如果结果集是

 值1值2 Value3 4 

我希望能够获得所有4条记录。

解决方案

你的意思是这样的:

  SELECT a。*,b.Count 
FROM table_name AS a
INNER JOIN(
SELECT Column1,Column2,Column3,Count(*)作为计数FROM表名
GROUP BY Column1,Column2 ,Column3
HAVING Count>'2'
)b
ON a.Column1 = b.Column1 A ND a.Column2 = b.Column2 AND a.Column3 = b.Column3

这基本上就是你在你的问题中描述,但在 JOIN 中。


Is there a way to expand/get all the records of a GROUP BY and HAVING query?

For example,

SELECT Column1, Column2, Column3, Count(*) as Count FROM table_name
GROUP BY Column1, Column2, Column3
HAVING Count > '2'

Is there an easier way to get all the records rather than going through the result set and doing SELECT * FROM table_name WHERE Column1 = 'this' AND Column2 = 'that' AND Column3 = 'more' for each record.

If it cannot be done due to mysql or some other restrictions is there any other way to get all the data for query above?

By expand/get all the records, I mean if the result set is

Value1 Value2 Value3 4

I want to be able to get all the 4 records.

解决方案

Do you mean something like this:

SELECT a.*, b.Count
FROM table_name AS a
INNER JOIN (
    SELECT Column1, Column2, Column3, Count(*) as Count FROM table_name
    GROUP BY Column1, Column2, Column3
    HAVING Count > '2'
) b
ON a.Column1 = b.Column1 AND a.Column2 = b.Column2 AND a.Column3 = b.Column3

This is basically what you described in your question but in a JOIN.

这篇关于展开GROUP BY和HAVING结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆