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

  • SEARCH
  • Home
  • 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 8525853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:06:08+00:00 2026-06-11T08:06:08+00:00

I am trying to select DISTINCT products from within categories with category ids (1,

  • 0

I am trying to select DISTINCT products from within categories with category ids (1, 5, 12), ORDERED by cat_order + prod_order from MySQL database

The problem:

if a product is found in more than 1 category I need to show the first result,

ie: product number 1 is assigned to categories 1 and 5, I need to display product number 1 from category 1 along with its prod_order and skip the listing in category 5,

  • essentually I need to display all products from category 1, than move on to category 5 and display all products from there, where product id was not shown previously, and move on to another category in the list (12)

if I run something like:

SELECT
    prod_to_cat.prod_id AS prod_to_cat_prod_id,
    prod_to_cat.prod_order AS prod_to_cat_prod_order,
    prod_to_cat.cat_id AS prod_to_cat_cat_id,
    prod_to_cat.cat_order AS prod_to_cat_cat_order,
    products.id,
    products.name

FROM
    prod_to_cat, products

WHERE
    prod_to_cat.prod_id = products.id
AND prod_to_cat.cat_id IN (1, 5, 12)
GROUP BY 
    prod_to_cat.prod_id
ORDER BY
    prod_to_cat_cat_order ASC,
    prod_to_cat_prod_order DESC

I get inconsistent results (product 1 will not be selected from the first category in the list), that is why I opted to select without “GROUP BY prod_id” and wrap that with another select which than groups by prod_id.

like so:

SELECT
    prod_to_cat_prod_id,
    prod_to_cat_prod_order,
    prod_to_cat_cat_id,
    name
FROM
    (
        SELECT
            prod_to_cat.prod_id AS prod_to_cat_prod_id,
            prod_to_cat.prod_order AS prod_to_cat_prod_order,
            prod_to_cat.cat_id AS prod_to_cat_cat_id,
            prod_to_cat.cat_order AS prod_to_cat_cat_order,
            products.id,
            products.name
        FROM
            prod_to_cat, products
        WHERE
            prod_to_cat.prod_id = products.id
        AND prod_to_cat.cat_id IN (1, 5, 12)
        ORDER BY 
            prod_to_cat_cat_order ASC,
            prod_to_cat_prod_order DESC
    ) AS prod
GROUP BY
    prod_to_cat_prod_id
ORDER BY
    prod_to_cat_cat_order ASC,
    prod_to_cat_prod_order DESC
LIMIT 0, 10;

What I am trying to do:

I am trying to find a more efficiant way to do this.

Table structure:

prod_to_cat:

prod_id | cat_id | cat_order | prod_order   |
    1        1          1           2
    2        1          1           0
    3        1          1           0
    1        5          2           4
    4        5          2           0


products:


id  |   name |  descr   |   price   |
 1    name_1
 2    name_2
 3    name_3
 4    name_4

each product can be in any number of categories, for example product id 1 is in categories id 1 and 5 in the example above.

Thanks a lot for any replies.
Pasha

  • 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-11T08:06:09+00:00Added an answer on June 11, 2026 at 8:06 am

    You want the groupwise minimum:

    SELECT   prod_to_cat.*,
             products.name
    FROM     prod_to_cat NATURAL JOIN (
               SELECT   prod_id,
                        MIN(cat_id) AS cat_id
               FROM     prod_to_cat
               WHERE    cat_id IN (1, 5, 12)
               GROUP BY prod_id
             ) t
        JOIN products ON t.prod_id = products.id
    ORDER BY prod_to_cat.cat_order  ASC,
             prod_to_cat.prod_order DESC
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I am trying to optimize the following modified MySQL query from OsCommerce: select distinct
So I'm running MySQL version 5.1.4 and I'm trying to select distinct by a
Im trying to replicate the following sql in Nhibernate ICriteria SELECT DISTINCT AP.ID FROM
I'm trying to select two distinct numbers id1 and id2 from the following table:
I am trying to do the following query: cursor.execute(SELECT DISTINCT(provider) FROM raw_financials WHERE vendor_id=%s
I am trying to generate SELECT DISTINCT P.DOMAIN_ID, P.SOURCE_SYSTEM_ID FROM EDW.dbo.DOMAIN_VALUE AS P WHERE
I'm trying to select DISTINCT rows from a view using ROW_NUMBER() OVER for paging.
Trying to select distinct rows from a dt, and put the results back in
I'm trying to select distinct substring values of a field and count the number
I am trying to execute the following Query select distinct pincode as Pincode,CAST(Date_val as

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.