【问题标题】:select all products from child categories in parent category从父类别的子类别中选择所有产品
【发布时间】:2017-12-24 12:36:07
【问题描述】:

我有以下“类别”表:

+--------+---------------+----------------------------------------+
| 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 类别,这是 Smartphones 类别的子类别。如果我替换'?在我对 13(Apple 的类别 ID)的查询中,我得到了产品。当我替换'?在我使用 12(Smartphones 的类别 ID)的查询中,我没有得到产品。我想选择该类别或子/孙/...类别之一中的所有产品。如何使用单个查询(如果可能)做到这一点?

【问题讨论】:

标签: php mysql


【解决方案1】:

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

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

【讨论】:

  • 我尝试了您的解决方案,但仍然得到相同的结果。
【解决方案2】:

可以使用join来实现

选择 ID, 姓名 从 产品 加入 类别 上 products.category_id = 类别.id WHERE products.category_id = 13 或 categories.parent_id = 12

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      1) 一个查询。我正在从这个答案中查询。 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";
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-28
        • 1970-01-01
        • 1970-01-01
        • 2015-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多