问题
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