Expand a GROUP BY and HAVING resultset

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:39:48

问题


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:


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.



来源:https://stackoverflow.com/questions/7165330/expand-a-group-by-and-having-resultset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!