从父类别中的子类别中选择所有产品

时间:2017-12-24 12:36:08

标签: php mysql

我有以下'类别'表:

+--------+---------------+----------------------------------------+
| ID     | Parent ID     | Name                                   |
+--------+---------------+----------------------------------------+
| 1      | 0             | Computers                              |
| 2      | 1             | Apple                                  |
| 3      | 1             | HP                                     |
| 4      | 2             | Macbook Air                            |
| 5      | 2             | Macbook Pro                            |
| 6      | 1             | Dell                                   |
| 7      | 6             | Inspiron                               |
| 8      | 6             | Alienware                              |
| 9      | 8             | Alienware 13                           |
| 10     | 8             | Alienware 15                           |
| 11     | 8             | Alienware 17                           |
| 12     | 0             | Smartphones                            |
| 13     | 12            | Apple                                  |
| 14     | 12            | Samsung                                |
| 15     | 12            | LG                                     |
+--------+---------------+----------------------------------------+

假设我有以下'产品'表:

+--------+---------------+----------------------------------------+
| ID     | Category ID   | Name                                   |
+--------+---------------+----------------------------------------+
| 1      | 13            | Apple iPhone 8                         |
| 2      | 13            | Apple iPhone 8 Plus                    |
| 3      | 14            | Samsung Galaxy S8                      |
+--------+---------------+----------------------------------------+

通过以下查询,我选择了一个类别中的所有产品:

SELECT
    id,
    name
FROM
    products
WHERE
    category_id = ?

好的,我的问题:

产品'Apple iPhone 8'属于 Apple 类别,这是智能手机类别的子类别。如果我更换'?'在我的13查询( Apple 的类别ID)中,我得到了产品。当我更换'?'在我的查询12(智能手机的类别ID ),我没有得到该产品。我想选择所有类别中或其中一个子/孙/ ......类别的产品。如何使用单个查询(如果可能)执行此操作?

4 个答案:

答案 0 :(得分:0)

您可以使用join。 你的query应该是这样的

SELECT
    id,
    name
FROM
    products
JOIN 
    categories
ON 
    products.category_id = categories.id;

答案 1 :(得分:0)

可以使用join

来实现

选择     ID,     名称 从     制品 加入     类别 上     products.category_id = categories.id WHERE products.category_id = 13 OR categories.parent_id = 12

答案 2 :(得分:0)

SELECT id,name FROM products LEFT JOIN categories ON products.category_id = categories.id

答案 3 :(得分:0)

1)一个QUERY。我正在接受这个答案的询问。 How to create a MySQL hierarchical recursive query请阅读以获取查询的完整说明。该查询假定父ID将小于子ID(如19小于20,21,22)。

select * from products where `Category ID` in 
    (select ID from 
    (select * from categories order by `Parent ID`, ID) categories_sorted, 
    (select @pv := '12') initialisation 
    where (find_in_set(`Parent ID`, @pv) > 0 
    and @pv := concat(@pv, ',', ID)) or ID = @pv)

您必须将“12”设置为父类别。

2)通过PHP中的两个部分,一个循环直到你拥有所有类别ID。然后是第二部分,获取这些类别中的所有产品。这更加冗长,但我喜欢你能清楚地看到发生了什么。

$db = new mysqli(host, user, password, database);

$all_ids = []; // total ids found, starts empty
$new_ids = [12]; // put parent ID here

do {

    // master list of IDs
    $all_ids = array_merge($new_ids, $all_ids);

    // set up query
    $set = "(".implode($new_ids, ',').")";
    $sql = "select ID from categories where `Parent ID` in $set";

    // find any more parent IDs?
   $new_ids = []; // reset to nothing
   $rows = $db->query($sql) or die("DB error: (" . $db->errno . ") " . $db->error);
    while ($row = mysqli_fetch_assoc($rows)) {
        $new_ids[] = $row['ID'];
    }

} while (count($new_ids) > 0);    


// get products
$set = "(".implode($all_ids, ',').")";
$sql = "select * from products where `Category ID` in $set";
$rows = $db->query($sql) or die("DB error: (" . $db->errno . ") " . $db->error);
while ($row = mysqli_fetch_assoc($rows)) {
    echo "{$row['Name']}<br>\n";
}
相关问题