Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8214483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:28:45+00:00 2026-06-07T11:28:45+00:00

I have a problem with implementing a module where one project can belong to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:28:47+00:00Added an answer on June 7, 2026 at 11:28 am

    No, this is fine. This is just one of the rare cases when you want to use the DISTINCT key word to remove duplicates.

    In this case this is justified by the fact that the logic of the query is correct, even though it returns more than one row. Many times one can see the usage of DISTINCT when actually the logic of the query is wrong.

    Side-note:

    • any filter on a table reference you’re using in the WHERE clause other than IS NULL/IS NOT NULL would make any LEFT JOIN on this same table reference turn to an INNER JOIN, as for the final resultset behaviour. (see this: https://stackoverflow.com/a/15483895/1291428)
    • you ought not use GROUP BY to simulate the effect of DISTINCT, for 2 reasons:

      1/ This is just not the purpose. One of the effects of GROUP BY is to eliminate duplicates, but its main purpose is to group rows according to a certain set of criteria, in order to apply some analytic calculations/operations on them.

      2/ GROUP BY also ORDER BY the results (in mysql), which is not necessarly what you want and in that case slows down the execution. Please, just ensure appropriate use of what the engines are providing, that’s always better from the point of view of forward compatibility. (anticipating that what you include as granted is actually not)

    regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem in implementing Mashups in PHP. Now I am using PHP
I have a problem implementing this logic in my project. Im using ServiceLoader and
We have a strange problem when implementing sessions with ColdFusion in IE6. After login
I have problem developing with live555. I already build the lib-files and example projects
I'm implementing a Skein hash function in Java, and I have a problem with
For one of my course project I started implementing Naive Bayesian classifier in C.
i have a little problem implementing some serialization/deserialization logic. I have several classes that
I have a slight problem implementing vibration functionality in my application. My code is:
I'm implementing the auth module in Kohana, and I can't seem to figure out
I have a problem implementing dropdownchoice in my code. I want to display a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.