如何使用mysql在分层数据模式中进行此SELECT?

时间:2015-10-10 03:09:20

标签: mysql sql database select

我使用分层数据模式将产品存储在数据库中。产品只有main和subcategory,这是我检索Full Tree of Products时的结果。

SELECT t1.name AS lev1,
             t2.name as lev2, 
             t3.name as lev3,
FROM categories AS t1
    LEFT JOIN categories AS t2 
        ON t2.parent = t1.category_id
    LEFT JOIN categories AS t3 
        ON t3.parent = t2.category_id
WHERE t1.name = 'Products'

+----------+----------------------+-------------------+
| lev1     | lev2                 | lev3              |
+----------+----------------------+-------------------+
| Products | Computers            | Laptops           |
| Products | Computers            | Desktop Computers |
| Products | Computers            | Tab PCs           |
| Products | Computers            | CRT Monitors      |
| Products | Computers            | LCD Monitors      |
| Products | Computers            | LED Monitors      |
| Products | Mobile Phones        | LG Phone          |
| Products | Mobile Phones        | Anroid Phone      |
| Products | Mobile Phones        | Windows Mobile    |
| Products | Mobile Phones        | iPad              |
| Products | Mobile Phones        | Samsung Galaxy    |
| Products | Digital Cameras      | test              |
| Products | Printers and Toners  | NULL              |
| Products | test                 | abc               |
| Products | test2                | NULL              |
| Products | test3                | NULL              |
| Products | Computer Accessaries | USB Cables        |
| Products | Computer Accessaries | Network Cables    |
+----------+----------------------+-------------------+

我的问题是我需要选择level2和level3类别ID以及此选择查询。

我试过这样的事情:

SELECT t1.name AS lev1,
                 t2.name as lev2, 
                 t3.name as lev3,
                 t3.category_id
FROM categories AS t1
    LEFT JOIN categories AS t2 
        ON t2.parent = t1.category_id
    LEFT JOIN categories AS t3 
        ON t3.parent = t2.category_id
WHERE t1.name = 'Products'

但它只提供level3 ID。像这样。

+----------+----------------------+-------------------+-------------+
| lev1     | lev2                 | lev3              | category_id |
+----------+----------------------+-------------------+-------------+
| Products | Computers            | Laptops           |           3 |
| Products | Computers            | Desktop Computers |           4 |
| Products | Computers            | Tab PCs           |           5 |
| Products | Computers            | CRT Monitors      |           6 |
| Products | Computers            | LCD Monitors      |           7 |
| Products | Computers            | LED Monitors      |           8 |
| Products | Mobile Phones        | LG Phone          |          10 |
| Products | Mobile Phones        | Anroid Phone      |          11 |
| Products | Mobile Phones        | Windows Mobile    |          12 |
| Products | Mobile Phones        | iPad              |          13 |
| Products | Mobile Phones        | Samsung Galaxy    |          14 |
| Products | Digital Cameras      | test              |          21 |
| Products | Printers and Toners  | NULL              |        NULL |
| Products | test                 | abc               |          20 |
| Products | test2                | NULL              |        NULL |
| Products | test3                | NULL              |        NULL |
| Products | Computer Accessaries | USB Cables        |          23 |
| Products | Computer Accessaries | Network Cables    |          24 |
+----------+----------------------+-------------------+-------------+

有人能告诉我怎样才能获得level2和level3?

2 个答案:

答案 0 :(得分:1)

如果您的方法如果产品只有第二级别类别而非3级别类别,您可以尝试

SELECT t1.name AS lev1,t2.name as lev2, t3.name as lev3,COALESCE( t3.category_id, t2.category_id ) FROM categories AS t1
LEFT JOIN categories AS t2 ON t2.parent = t1.category_id
LEFT JOIN categories AS t3 ON t3.parent = t2.category_id WHERE t1.name = 'Products'

如果没有任何3级别类别

,则会为您提供级别2的类别ID

答案 1 :(得分:1)

t2.category_id,

添加到您的字段中
SELECT t1.name AS lev1,
                 t2.name as lev2, 
                 t2.category_id, 
                 t3.name as lev3,
                 t3.category_id
FROM categories AS t1
    LEFT JOIN categories AS t2 
        ON t2.parent = t1.category_id
    LEFT JOIN categories AS t3 
        ON t3.parent = t2.category_id
WHERE t1.name = 'Products'