简体   繁体   English

从具有一对多关系的数据库中获取特定产品

[英]Getting specific products from database with one-to-many relationship

I am making a products catalog using mySQL and PHP framework Codeigniter 4. Here I will show a simplified example.我正在使用 mySQL 和 PHP 框架制作产品目录 Codeigniter 4. 这里我将展示一个简化的示例。 I have two tables in my database.我的数据库中有两个表。 One has common data like product name and price.一个有产品名称和价格等通用数据。 The other has various attributes like color, material etc. So one product has many attributes.另一个有颜色、材料等各种属性。所以一个产品有很多属性。 Something like that:像这样的东西:


Products产品

| | id -- |编号—— | name ----- |名称 ----- | price -- |价格 -- |

| | 0 --- | 0 --- | Chair 1 -- |椅子 1 -- | 50 ----- | 50 ----- |

| | 1 --- | 1 --- | Chair 2 -- |主席 2 -- | 75 ----- | 75 ----- |


Attributes属性

| | id -- |编号—— | product_id -- |产品编号——| attribute -- |属性——| value -- |价值——|

| | 0 --- | 0 --- | 0 ------------ | 0 ---------- | color ------ |颜色 ------ | black -- |黑色——|

| | 1 --- | 1 --- | 0 ------------ | 0 ---------- | size -------- |尺寸 -------- | small -- |小—— |

| | 2 --- | 2 --- | 1 ------------ | 1 ---------- | color ------ |颜色 ------ | white -- |白色—— |

| | 3 --- | 3 --- | 1 ------------ | 1 ---------- | size -------- |尺寸 -------- | large -- |大—— |


My problem involves getting specific products, as when there is a filter of products and the user wants to see all products with certain attributes.我的问题涉及获取特定产品,因为当有产品过滤器并且用户想要查看具有特定属性的所有产品时。 If I need to get one product with one attribute, I have no problem with that.如果我需要获得具有一种属性的一种产品,我对此没有问题。 I do this:我这样做:

$product = $productsModel->join('attributes', 'attributes.product_id = products.id')
                         ->where('attribute', 'color')
                         ->where('value', 'black')
                         ->find();

However I cannot get products according to multiple attributes.但是我无法根据多个属性获取产品。 Like if I need a chair that is black and small, I cannot add ->where('attribute', 'size')->where('value', 'small') because color and size are different rows.就像如果我需要一把黑色小的椅子,我不能添加->where('attribute', 'size')->where('value', 'small')因为颜色和尺寸是不同的行。 It is easy to see if I use the same join and try to get the product with id 0. The result is:很容易看出我是否使用相同的join并尝试获取id为0的产品。结果是:

Array
    (
        [0] => Array
            (
                [id] => 0
                [name] => Chair 1
                [price] => 50
                [product_id] => 1
                [attribute] => color
                [value] => black
            )    
        [1] => Array
            (
                [id] => 1
                [name] => Chair 1
                [price] => 50
                [product_id] => 0
                [attribute] => size
                [value] => small
            )    
    )

Any idea how to solve this problem?知道如何解决这个问题吗? I need to create a filter with checkboxes that will display all available attributes of products and allow users to search for any combination.我需要创建一个带有复选框的过滤器,该过滤器将显示产品的所有可用属性并允许用户搜索任何组合。

In raw MySQL your current query reads:在原始 MySQL 中,您当前的查询为:

SELECT products.*
FROM products 
INNER JOIN attributes ON attributes.product_id = products.id
WHERE attribute = 'color' AND value = 'black';

Adding a second join could look something like this:添加第二个连接看起来像这样:

SELECT P.*
FROM products AS P
INNER JOIN attributes AS A1 ON A1.product_id = P.id
INNER JOIN attributes AS A2 ON A2.product_id = P.id
WHERE A1.attribute = 'color' AND A1.value = 'black' AND
      A2.attribute = 'size'  AND A2.value = 'small';

See: https://www.mysqltutorial.org/mysql-inner-join.aspx参见: https://www.mysqltutorial.org/mysql-inner-join.aspx

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

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