Forum Moderators: open
GROUP BYclause, there are certain rules that apply for grouping columns. One of those rules is "The Single-Value Rule" -- every column named in the
SELECTlist must also be a grouping column unless it is an argument for one of the set functions. MySQL extends standard SQL by allowing you to use columns or calculations in a
SELECTlist that don't appear in a
GROUP BYclause. The MySQL docs once warned not to use this feature unless the columns you omit from the
GROUP BYclause are not unique in the group because you will get unpredictable results. Times may have changed, but I believe it is still good practice to write standard SQL queries.
SELECT
id,
product_img,
COUNT(id) AS total
FROM products
GROUP BY
id,
product_img
HAVING COUNT(id) > 1
;
COUNT(id)
2.) MySQL counts are optimized for count(*) if I remember correctly (double check), but I think I remember reading the count of indexed data is optimized for a count(*) over a specific col. (I don't remember all the ins and outs, but I know for certain queries related to counts it is -or was- better to use the * than something more specific because of the way an index is created.)