mysql:选择菜单 - 取消选择结果中不存在的所有类别

发布于 2024-12-25 18:22:06 字数 1515 浏览 1 评论 0原文

使用 mysql (和 php)我尝试隐藏在选择菜单之外 不可用于项目选择的类别。 基本上我被困住了,没有办法做到这一点。

Asos 使用此技术浏览其中的产品 ( http://www .asos.com/Men/Jeans/Cat/pgecategory.aspx?cid=4208)。

需要明确的是,问题不在于如何执行 ajax 操作,而在于如何有效地使用 mysql 和 php 来构建这样的系统。

我试图弄清楚如何使用嵌套集来做到这一点:

    (1)items(22)----------------------------------
          |                           |
          |                           |
    (2)producs(7)                  (8)origin(21)
          |                           |
(3)single(4)-(5)bundle(6)             |
                             ---------------------
                             |                   |
                            (9)EU(14 )        (15)ASIA(20)   
                             |                   |   
                          ----------------      -----------------
                          |             |         |              |
                        (10)DU(11)  (12)PL(13)  (16)CN(17)   (18)JP(19)

菜单:
类型:
-单
-捆绑

来源:
-欧盟
-亚洲

国家:
-DU
-PL
-CN
-JP

假设数据库中有一项: 项目“1”位于:
-单
-欧盟
-DU

项目类别表:

itemID - cat_lft - cat_rgt
  1        10        11
  1        7         12
  1        2         3

在选择 DU 的菜单中应选择:
DU-EU-SINGLE

并取消选择其余部分(返回数组?),因为目前只有一项。

您可以获取项目并比较结果是否存在于其余类别中,但这听起来像是使用了大量查询...

有什么建议从这里开始吗?

Using mysql (and php) I try to hide out of a select menu
category's that are not available for the item selection.
Basically I'm stuck and can't thing of a way to do it.

Asos uses this technique to browse trough there products ( http://www.asos.com/Men/Jeans/Cat/pgecategory.aspx?cid=4208 ).

To be clear the problem is not how to do the ajax stuff but how efficiently use mysql and php to make a system like this.

I've tried to figure out to do this with nested sets:

    (1)items(22)----------------------------------
          |                           |
          |                           |
    (2)producs(7)                  (8)origin(21)
          |                           |
(3)single(4)-(5)bundle(6)             |
                             ---------------------
                             |                   |
                            (9)EU(14 )        (15)ASIA(20)   
                             |                   |   
                          ----------------      -----------------
                          |             |         |              |
                        (10)DU(11)  (12)PL(13)  (16)CN(17)   (18)JP(19)

Menu:
type:
-single
-bundle

origin:
-EU
-ASIA

country:
-DU
-PL
-CN
-JP

Lets say there's one item in the db:
item '1' is in:
-single
-EU
-DU

Item category table:

itemID - cat_lft - cat_rgt
  1        10        11
  1        7         12
  1        2         3

In the menu selecting DU should select:
DU-EU-SINGLE

And deselected the rest (return a array?) because theres is only one item for now.

You could get the items and compare if the result are present in the remaining category's, but that sounds like using a lot of query's...

Any advice where to go from here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2025-01-01 18:22:06

我就是这样做的。确保您的产品仅按叶子分类(即左 = 右 + 1)。因此,产品可以归类为“DU”,但不能归类为“原产地”或“ASIA”。

类别表:

  • category_id
  • name
  • left
  • right

Products 表:

  • product_id
  • name
  • ...

Categories_Products 表:

  • category_id
  • Product_id

SQL 查询来确定选中的 vs未选中的类别:

SELECT
    `categories`.`category_id`,
    `categories`.`name`,
    IFNULL(COUNT(`categories_products`.`id`), 0) AS `count` FROM `categories`

LEFT JOIN `categories` as `children` ON
    `children`.`left`  > `categories`.`left`  AND
    `children`.`right` < `categories`.`right`

JOIN `categories_products` ON
    `categories_products`.`category_id` = `children`.`category_id`

GROUP BY `categories`.`category_id`

如果“count”为0,则不选中;否则,它会被检查,并且您知道该类别中有多少项目。这尚未经过测试,因此可能需要一些调整。您肯定需要在category_id、product_id、左列和右列上建立索引。

添加:用于选择一个或多个类别中的产品的 SQL 查询(复合条件见下文):

SELECT `products`.* FROM `categories`

JOIN `categories` as `children` ON
    `children`.`left`  >= `categories`.`left`  AND
    `children`.`right` <= `categories`.`right`

JOIN `categories_products` ON
    `categories_products`.`category_id` = `children`.`category_id`

JOIN `products` ON
    `products`.`product_id` = `categories_products`.`product_id`

WHERE `categories`.`category_id` IN (1,2,3,n,...)

GROUP BY `products`.`product_id`

添加:用于选择多个类别中的产品的逻辑,复合过滤(产品必须属于所有类别才能是匹配的):

您必须针对您想要的每个类别要求进行单独的联接。请注意,这种类型的查询可能会变得非常慢,因此请随时进行一些基准测试。您可能最好进行多个查询(每个类别一个),并将结果合并到应用程序代码中。

沿着同样的思路,我建议在产品表中使用“单一与捆绑”之类的 ENUM,并将分类留给更抽象的结构(区域有效,传统的面向消费者的“逻辑”结构也可以)分类结构),这不是一个简单的非此即彼标志。

Here's how I would do it. Make sure your products are categorized with leaves only (i.e. left = right + 1). So, a product can be categorized as "DU," but not "origin" or "ASIA."

Category table:

  • category_id
  • name
  • left
  • right

Products table:

  • product_id
  • name
  • ...

Categories_Products table:

  • category_id
  • product_id

SQL query to determine checked vs. unchecked categories:

SELECT
    `categories`.`category_id`,
    `categories`.`name`,
    IFNULL(COUNT(`categories_products`.`id`), 0) AS `count` FROM `categories`

LEFT JOIN `categories` as `children` ON
    `children`.`left`  > `categories`.`left`  AND
    `children`.`right` < `categories`.`right`

JOIN `categories_products` ON
    `categories_products`.`category_id` = `children`.`category_id`

GROUP BY `categories`.`category_id`

If "count" is 0, leave it unchecked; otherwise, it's checked, and you know how many items are in that category. This is untested, so it might need some tweaking. You DEFINITELY want indexes on category_id, product_id, left, and right columns.

Addition: SQL query to select products in one or more categories (see below for compound conditions):

SELECT `products`.* FROM `categories`

JOIN `categories` as `children` ON
    `children`.`left`  >= `categories`.`left`  AND
    `children`.`right` <= `categories`.`right`

JOIN `categories_products` ON
    `categories_products`.`category_id` = `children`.`category_id`

JOIN `products` ON
    `products`.`product_id` = `categories_products`.`product_id`

WHERE `categories`.`category_id` IN (1,2,3,n,...)

GROUP BY `products`.`product_id`

Addition: Logic to select products in multiple categories, compound filtered (product must be in all categories to be a match):

You'll have to do separate joins for every category requirement you want. Please note that this type of query could get pretty slow, so do some benchmarking as you go. You might be better off with multiple queries (one for each category), combining the results in the application code.

Along those same lines, I'd recommend that something like "single vs. bundle" be an ENUM in your products table, and categorization is left to more abstract structures (region works, and so would a traditional consumer-facing, "logical" categorization structure) that aren't a simple either/or flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文