I need to get the bottom N values from a table taking into account that multiple categories or types are inserted.
my table:
CREATE TABLE `Table1` (
`name` VARCHAR(250) NOT NULL,
`total` INT(11) NOT NULL,
`type` INT(11) NOT NULL
)
ENGINE=InnoDB;
Some values:
|| *name* || *total* || *type* ||
|| Clock || 12 || 103 ||
|| Brief Case || 21 || 103 ||
|| Pencil || 34 || 103 ||
|| Lollypop || 45 || 103 ||
|| Notebook || 67 || 142 ||
|| Rubber Band || 3 || 143 ||
|| Smartphone || 1 || 143 ||
What I got so far:
SELECT name,total from Table1 ORDER BY TOTAL ASC LIMIT 3 where type=143;
but I need the same result for every category with one query.
Tried this:
SELECT name,total from Table1 ORDER BY TOTAL ASC LIMIT 3 GROUP BY type;
but this is wrong. As expected, I retrieve just 3 records from all records instead 3 from every category.
[possible duplicate] Mysql selecting (n) rows of each occurrance