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 7514633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:33:24+00:00 2026-05-30T00:33:24+00:00

This is my first post on this forum (so please be gentle ) I’m

  • 0

This is my first post on this forum (so please be gentle )

I’m working on a huge database of products and I have got many tables of infos about them.

There is one table that looks like this:

product

  • product_id
  • name
  • brand

And another table that has the different types of the same product which looks like this:

product_type

  • type_id
  • type
  • costs
  • product_id

I use the join to select both tables and it works fine, but since there are more than a thousand entries in both tables I want to limit the selections and show only 15 entries on a page.

So here comes my problem:

I select the two tables joined and limit them 15. But since there are more types than products I select 15 product types which belong to an unknown number of products (since one product can have 1, 2 or any number of types). I need to be able to search in both tables and order the results according to columns from both and it’s not good either to just select 15 entries from products and one entry from product_types for each, because I need all types to display…

So can you help me what should I do? I’m kinda stuck with it.
(I hope my description is understandable)

  • 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-05-30T00:33:25+00:00Added an answer on May 30, 2026 at 12:33 am

    There are a number of ways to achieve this. The main difficulty as I see it is that you need to order on columns from both tables. Thus the first 15 distinct products will appear in a variable number of rows given the relationship with the types. The easiest way might be with a GROUP_CONCAT

    SELECT
        p.*,GROUP_CONCAT(t.type)
    FROM
        products p INNER JOIN types t ON p.product_id=t.product_id
    WHERE
        [your WHERE clauses]
    GROUP BY
        p.product_id
    ORDER BY
        [columns]
    LIMIT 15
    

    However this gives you limited data about your types and may give you problems in your ORDER BY as this happens after the GROUP.

    You probably want to do something like this

    SELECT
        *
    FROM
        ( SELECT
            DISTINCT p.*
        FROM
            products p INNER JOIN types t ON p.product_id=t.product_id
        WHERE
            [your WHERE clauses]
        ORDER BY
            [columns]
        LIMIT 15 ) pl
    INNER JOIN types tl ON pl.product_id=tl.product_id
    

    What this should do is get the first 15 DISTINCT rows from your products table when joined on your types table and ORDERed by your criteria, and then get all types for those products.

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

Sidebar

Related Questions

This is my first post in any forum so please bear with me. I
This is my first post here. I have a problem. I need to take
This is my first post on Stack, so please bear with me if I
Well, this is my first post here and really enjoying the site. I have
This is my first post to Stack Overflow so please forgive me if I
This is my first post here and since I've seen many great answers I
This is my first post here and I wanted to get some input from
This is my first post and I'm quite a novice on C++ and compiling
This is my first post on Stack Overflow and I'm just wondering on the
this is my first post here on stackoverflow and am very impressed by the

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.