我在实现一个project
可以属于多个的模块时遇到问题categories
。示例:项目“PHP Programmer”属于类别:编程、PHP。
假设以下查询(选择属于类别 1,3,11 的项目):
SELECT projects.* FROM projects
LEFT JOIN pojects_category on projects.id = pojects_category.project_id
WHERE pojects_category.category_id IN (1,3,11) and projects.id='94'`
我得到一个相同的项目返回两次,因为表中有2 个匹配=project_category
project_id
94
表projects_category
架构:
CREATE TABLE IF NOT EXISTS `pojects_category` (
`project_id` int(10) NOT NULL,
`category_id` int(10) NOT NULL,
KEY `category_id` (`category_id`),
KEY `project_id` (`project_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `pojects_category` (`project_id`, `category_id`) VALUES
(94, 3),
(94, 1);
我错过了什么吗?
解决方案:使用GROUP BY
或DISTINCT