Expand a GROUP BY and HAVING resultset

2019-08-10 20:32发布

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.

1条回答
爷、活的狠高调
2楼-- · 2019-08-10 21:26

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.

查看更多
登录 后发表回答