我有这个 mysql 代码,可以通过它们在数据库中出现的次数来检索每个类别中最流行的描述 -
它抓取了列表,但它会像这样回显数据
"Adidas was the most popular in category 5 with 1 occurrences"
我的这个兴趣(爱好)的表结构是
# 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
这是我的 SQL 命令:
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
我收到此错误:
不是唯一的表/别名:“类别”