简体   繁体   中英

one product in many categories, finding n number of products from each category - php mysql

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.

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

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 .

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM