0

I have products belonging to multiple categories.

products         categories       products_categories
------------     -------------    --------------------
product_id       category_id      product_id
product_title    category_title   category_id

I want to get n number, could be lets say 6, of last inserted different products from each category. I would also make a join of products on it to fetch the product info, however that should be easy.

what i mean from different products is that if one product belongs to many categories, it should come only once in one category and other category should get another n, 6 products, if available else whatever available.

I could try something like defined here using the rank Select 2 products from each category in MySQL but this solution does not handle the condition when product belongs to multiple categories, and i want different n number of products in each category.

here is some sample data

pid catid    insertion_date
284 48  2/4/2013 1:14:16 AM
278 41  2/4/2013 1:25:03 PM
278 43  2/4/2013 1:25:03 PM
284 45  2/4/2013 12:55:24 PM
284 44  2/4/2013 12:55:24 PM
285 41  2/4/2013 1:28:37 AM
278 42  2/4/2013 1:25:03 PM
285 47  2/4/2013 1:28:42 AM
285 48  2/4/2013 12:50:44 PM
278 46  2/4/2013 1:25:03 PM
278 47  2/4/2013 1:25:03 PM
278 48  2/4/2013 1:25:03 PM
286 43  2/7/2013 3:20:40 PM
286 44  2/7/2013 3:20:40 PM
243 41  2/14/2013 3:30:59 PM
243 42  2/14/2013 3:30:59 PM
243 43  2/14/2013 3:30:59 PM
266 41  2/14/2013 3:51:40 PM
266 47  2/14/2013 3:51:40 PM
266 48  2/14/2013 3:51:40 PM
286 41  2/14/2013 3:51:55 PM
286 42  2/14/2013 3:51:55 PM
286 45  2/14/2013 3:51:55 PM
286 46  2/14/2013 3:51:55 PM
286 47  2/14/2013 3:51:55 PM
286 48  2/14/2013 3:51:55 PM
254 41  2/14/2013 3:52:07 PM
254 43  2/14/2013 3:52:07 PM
254 45  2/14/2013 3:52:07 PM
254 47  2/14/2013 3:52:07 PM
254 48  2/14/2013 3:52:07 PM
252 41  2/14/2013 3:52:23 PM
252 42  2/14/2013 3:52:23 PM
252 45  2/14/2013 3:52:23 PM
252 46  2/14/2013 3:52:23 PM
252 47  2/14/2013 3:52:23 PM
252 48  2/14/2013 3:52:23 PM
169 46  2/14/2013 3:55:54 PM
221 46  2/14/2013 3:56:08 PM

if somebody has a better solution to do such things in php, please suggest.

2
  • give us sample data with desired result so we can work it out.
    – John Woo
    Feb 14, 2013 at 14:59
  • added the sample data.
    – asm234
    Feb 14, 2013 at 15:10

3 Answers 3

1

This example will give you 2 latest product_title based on latest product_ID for every category_title.

SELECT  a.category_title, c.product_title
FROM    categories a
        INNER JOIN products_categories b
            ON a.category_ID = b.category_ID
        INNER JOIN products c
            ON b.product_ID = c.product_ID
WHERE   
        (
            SELECT  COUNT(*)
            FROM    products_categories d
            WHERE   b.category_ID = d.category_ID AND
                    c.product_ID <= d.product_ID
        ) <= 2
2
  • i think it is not good, none of your product is into the mutiple categories, however I will re-check it out in a while and update you. will it be good to fetch such query from thousands records?
    – asm234
    Feb 14, 2013 at 15:31
  • yes, it is not good enough, it fails if u have products into multiple categories and that is my basic requirement. i think we need a solution using rank but not sure how.
    – asm234
    Feb 14, 2013 at 15:36
0

My SQL is a bit rusty, but I guess you can use the NOT IN () to remove any duplicates. If you have a statement to get your first 6 products, then use something like:

WITH Statement1 as (SELECT .... FROM ....)  -- Get your first 6 products
WITH Statement2 as (SELECT ... FROM .... NOT IN (Statement1)) -- Get 6 new one, NOT in Statement1
... and so on ...

Not the prettiest code, but you should understand the concept of what I'm trying to say.

To get all in result then you can create a view to easily get them:

CREATE VIEW AllProducts AS
  Statement1 UNION Statement2 UNION ...;

SELECT .... FROM AllProducts;
4
  • it is difficult to make a single query out of your suggestion. what you saying is actually the description of the my problem not the solution. I can not fire multiple queries.
    – asm234
    Feb 14, 2013 at 15:12
  • Using the WITH (as I updated to) you should be able to join all statements in the end if you only want one list with all results. Feb 14, 2013 at 15:14
  • Sorry, that's wrong. Using the UNION you should be able to combine all statements. Edited my post. Feb 14, 2013 at 15:17
  • I understood what you saying, but it cant be done this way. here, we need to make inner query for each statement, also making statement 1 as a inner query for statement 2 seems not doable in union and mysql. even if we somehow do it, it is very expensive in nature.
    – asm234
    Feb 14, 2013 at 17:05
0

If you need a repeated value to be taken only once, refer to DISTINCT, which will return you a result with unique values for given column.

And to get the last inserted ones, ORDER BY, using DESC

To limit how many rows you want, LIMIT

So your final query is: SELECT DISTINCT product_id FROM products_categories ORDER BY product_id DESC LIMIT 6

Edit: Trying the actual way you meant it, so far can't get a proper result, yet I believe this will be doable with JOIN, however I can't just form it at the moment. I'll have a look at this again later on, until then good luck in finding, I'm also quite curious about this kind of output, so will be adding this to favorites.

3
  • this will give you 6 results only, not 6 results per category.
    – John Woo
    Feb 14, 2013 at 15:01
  • trust me, it is not that easy. your query will give distinct 6 records from all categories. I want n, 6, products from each category and that too only the last 6 inserted ones.
    – asm234
    Feb 14, 2013 at 15:03
  • It seems I misunderstood then, let me try that one. Feb 14, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.