出于某种原因,我的 SELECT 查询返回每一行的重复项。以下是我的表格的简要分类:
产品: products_id | master_categories_id | 产品型号 | 产品状态 | products_quantity
类别: categories_id | 父 ID
类别描述:类别 ID | 类别名称
这个查询没有问题...
***QUERY***
SELECT
categories_name AS category,
products_model AS model,
products_status AS status,
products_quantity AS qty
FROM products p
LEFT JOIN categories c
ON c.categories_id = p.master_categories_id
LEFT JOIN categories_description cd
USING (categories_id)
WHERE p.products_quantity > 0
AND p.products_status = FALSE;
***OUTPUT***
+-------------+------------------------+--------+-----+
| category | model | status | qty |
+-------------+------------------------+--------+-----+
| Network | CP-C-10-CAT6X0 | 0 | 5 |
| Network | CP-C-03-CAT6X0 | 0 | 5 |
| Batteries | BATT-LHP203 | 0 | 1 |
| Batteries | BATT-LIB201 | 0 | 1 |
| Refurbished | UC*DELL-OPT-760-DC-IBC | 0 | 9 |
| Desktop | HDD-320-SATA-3.5-PULL | 0 | 12 |
| Refurbished | UL*ASUS-S56CM-SH51 | 0 | 1 |
+-------------+------------------------+--------+-----+
但是,类别表包含一个 parent_id,它指示您在上面看到的类别的父类别(例如,电缆 > 网络)。这是我尝试包含所述主类别的名称:
***QUERY***
SELECT
parent_name AS parent,
categories_name AS category,
products_model AS model,
products_status AS status,
products_quantity AS qty
FROM products p
LEFT JOIN categories c
ON c.categories_id = p.master_categories_id
LEFT JOIN categories_description cd
USING (categories_id)
LEFT JOIN (
SELECT
parent_id,
categories_name AS parent_name
FROM categories c
LEFT JOIN categories_description cd
ON cd.categories_id = c.parent_id
) par
USING (parent_id)
WHERE p.products_quantity > 0
AND p.products_status = FALSE;
***OUTPUT***
+--------------+-------------+------------------------+--------+-----+
| parent | category | model | status | qty |
+--------------+-------------+------------------------+--------+-----+
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-10-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Cables | Network | CP-C-03-CAT6X0 | 0 | 5 |
| Laptop Parts | Batteries | BATT-LHP203 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LHP203 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LHP203 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LHP203 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LIB201 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LIB201 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LIB201 | 0 | 1 |
| Laptop Parts | Batteries | BATT-LIB201 | 0 | 1 |
| Desktop PCs | Refurbished | UC*DELL-OPT-760-DC-IBC | 0 | 9 |
| Desktop PCs | Refurbished | UC*DELL-OPT-760-DC-IBC | 0 | 9 |
| Hard Drives | Desktop | HDD-320-SATA-3.5-PULL | 0 | 12 |
| Hard Drives | Desktop | HDD-320-SATA-3.5-PULL | 0 | 12 |
| Hard Drives | Desktop | HDD-320-SATA-3.5-PULL | 0 | 12 |
| Hard Drives | Desktop | HDD-320-SATA-3.5-PULL | 0 | 12 |
| Laptops | Refurbished | UL*ASUS-S56CM-SH51 | 0 | 1 |
| Laptops | Refurbished | UL*ASUS-S56CM-SH51 | 0 | 1 |
+--------------+-------------+------------------------+--------+-----+
如您所见,每一行都重复了多次。我究竟做错了什么?