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)
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
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
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.