I have a problem with implementing a module where one project
can belong to multiple categories
. Example: project "PHP Programmer" belongs to categories: Programming, PHP.
Assuming the following query (select projects that belong to categories 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'`
I get a the same project returned twice, because there are 2 matches in the project_category
table for the project_id
= 94
Table projects_category
schema:
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);
Am I missing something?
Solution: use GROUP BY
or DISTINCT
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…