由于某种原因,我的SELECT查询返回每行的重复项。以下是我的表格的简要分类:
产品: products_id | master_categories_id | products_model | products_status | products_quantity
类别: categories_id | PARENT_ID
categories_descriptions: categories_id | categories_name
这个查询没问题......
***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 |
+-------------+------------------------+--------+-----+
但是,categories表包含parent_id,表示您在上面看到的类别的父类别(例如,Cables> Network)。这是我尝试包含所述主类别的名称:
***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 |
+--------------+-------------+------------------------+--------+-----+
如您所见,每行重复多次。我做错了什么?
答案 0 :(得分:1)
使用select distinct
是一个坏习惯。有时这是必要的,但不是在这种情况下。为什么删除重复项的额外工作,当它足够简单来修复查询时?
from
子句应如下所示:
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
categories_description cdp
on cdp.categories_id = c.parent_id
您的附加子查询不仅会引入额外的处理开销,还会引入重复项和不必要的查询复杂性。
答案 1 :(得分:0)
使用DISTINCT删除重复项
SELECT DISTINCT ...