简体   繁体   English

一个产品在多个类别中,从每个类别中查找n个产品-php mysql

[英]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. 我想获得n个数字,可以说6个,是每个类别中最后插入的不同产品。 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. 我从不同产品中获得的意思是,如果一个产品属于多个类别,则在一个类别中它应该只出现一次,而其他类别应获得n个,6个产品(如果有)。

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. 我可以尝试使用在MySQL中从每个类别中选择2个产品的排名来定义此处的内容但是当产品属于多个类别时,此解决方案无法处理这种情况,并且我希望每个类别中有n种不同的产品。

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. 如果有人有更好的解决方案在php中进行此类操作,请提出建议。

This example will give you 2 latest product_title based on latest product_ID for every category_title . 本示例将根据每个category_title最新product_ID为您提供2个最新product_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. 如果只需要重复一次的值,请参考DISTINCT ,它将为您返回具有给定列唯一值的结果。

And to get the last inserted ones, ORDER BY , using DESC 并使用DESC获得最后插入的ORDER BY

To limit how many rows you want, LIMIT 要限制所需的行数, LIMIT

So your final query is: SELECT DISTINCT product_id FROM products_categories ORDER BY product_id DESC LIMIT 6 因此,您的最终查询是: 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. 编辑:尝试使用实际的方式 ,到目前为止,尚无法获得正确的结果,但是我相信使用JOIN可以做到这一点,但是目前我还不能这样做。 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. 我的SQL有点生锈,但是我想您可以使用NOT IN()删除所有重复项。 If you have a statement to get your first 6 products, then use something like: 如果您有声明要购买您的前6种产品,请使用类似以下内容的产品:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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