kewl
kewl

Reputation: 54

mySQL Statement for group by with count over 1

Is there a faster version of the following SQL statement?

SELECT Name, Anzahl
FROM (

SELECT Name, count( * ) AS Anzahl
FROM `Product`
GROUP BY Name
)m
WHERE Anzahl >1

Upvotes: -1

Views: 149

Answers (2)

Bartłomiej Wach
Bartłomiej Wach

Reputation: 1986

You dont need 2 selcts, try this:

SELECT Name, count( * ) AS Anzahl
FROM `Product`
GROUP BY Name
HAVING Anzahl >1

what you did with 2 selcts was select EVERYTHING FROM (SELECT EVERYTHING) WHERE COUNT > 1 so why not make it SELECT EVERYTHING WHERE COUNT > 1 ;)

Hope this answers your question

Upvotes: 1

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You do not need another select the following should do what you need

SELECT Name, 
count( * ) AS Anzahl 
FROM Product 
GROUP BY Name 
having Anzahl > 1 

Upvotes: 2

Related Questions