I have this mysql code that retrieves the most popular descriptions in each category by how many times they appear in the database –
It grab’s the list however it echo’s out the data like this
"Adidas was the most popular in category 5 with 1 occurrences"
My table structure for this interests (hobbies) is
# interests
CREATE TABLE `interests` (
`interestID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`categoryID` int(11) NOT NULL,
`sessionID` int(11) NOT NULL,
`interest_desc` varchar(30) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`interestID`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
# categories
CREATE TABLE `categories` (
`categoryID` int(11) NOT NULL AUTO_INCREMENT,
`category_desc` varchar(100) NOT NULL,
PRIMARY KEY (`categoryID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
This is my SQL command:
SELECT
interest_desc,
categoryID,
MAX(num_in_cat) AS num_in_cat,
category_name
FROM
(
SELECT interest_desc, categoryID, COUNT(categoryID) AS num_in_cat
FROM interests
GROUP BY interest_desc, categoryID
) subsel
JOIN categories ON subsel.categoryID = categories.ID
GROUP BY interest_desc, categoryID, category_name
I’m receiving this error:
Not unique table/alias: ‘categories’
An inner join against your
categoriestable (you supply the correct table and column names) is all you need to complete this: