大家好。
以下示例显然是一个极端的伪示例,但它表示我很好地面对的问题。
我已经为此苦苦挣扎了一段时间,但我还没有找到一个好的解决方案。
我需要打印出所有类别的列表(父类别也是如此!其子类别的总销售额总计)以及产品的总销售额-按总销售额排序。
我需要打印的内容
+-------------+---------------+-------+
| category_id | category | sales |
+-------------+---------------+-------+
| 1 | shirts | 1100 |
| 2 | t-shirts | 1000 |
| 3 | tank-tops | 100 |
| 4 | hats | 100 |
| 5 | baseball caps | 50 |
| 6 | beanies | 50 |
+-------------+---------------+-------+
表格
Table: categories
+-------------+--------------+
| category_id | category |
+-------------+--------------+
| 1 | shirts |
| 2 | t-shirts |
| 3 | tank-tops |
| 4 | hats |
| 5 | baseball caps|
| 6 | beanie |
+-------------+--------------+
Table: categories_to_categories
+-----------------------------+---------------+----------------------+
| category_to_category_id | category_id | parent_category_id |
+-----------------------------+---------------+----------------------+
| 1 | 1 | 0 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 4 | 0 |
| 5 | 5 | 4 |
| 6 | 6 | 4 |
+-----------------------------+---------------+----------------------+
Table: products
+------------+-------------+------------------+-------+
| product_id | category_id | name | sales |
+------------+-------------+------------------+-------+
| 1 | 2 | black t-shirt | 600 |
| 2 | 2 | blue t-shirt | 400 |
| 3 | 3 | white tank-top | 100 |
| 4 | 5 | red baseball cap | 50 |
| 5 | 6 | yellow beanie | 50 |
+------------+-------------+------------------+-------+
这有可能吗?我已经在整个PHP中尝试了一些递归功能,但是它非常缓慢,而且肯定不是最佳方法。
答案 0 :(得分:2)
使用聚合函数并加入
select c.category_id,
c.category ,sum(p.sales) as sum_sales from
product p inner join
categories_to_categories ctc
on p.category_id=ctc.category_id
inner join
categories c on ctc.parent_category_id=c.category_id
group by c.category_id, c.category
order by sum_sales desc
答案 1 :(得分:2)
在表模式中,您需要使用UNION ALL
编写一个子查询,以合并来自categories_to_categories
的两个结果集
parent_category_id
的价格category_id
的价格然后根据OUTER JOIN
进行categories
。
CREATE TABLE categories( category_id INT, category VARCHAR(50));
INSERT INTO categories VALUES (1 ,'shirts');
INSERT INTO categories VALUES (2 ,'t-shirts');
INSERT INTO categories VALUES (3 ,'tank-tops');
INSERT INTO categories VALUES (4 ,'hats');
INSERT INTO categories VALUES (5 ,'baseball caps');
INSERT INTO categories VALUES (6 ,'beanie');
CREATE TABLE categories_to_categories(
category_to_category_id INT,
category_id INT,
parent_category_id INT
);
INSERT INTO categories_to_categories VALUES ( 1 ,1 , 0 );
INSERT INTO categories_to_categories VALUES ( 2 ,2 , 1 );
INSERT INTO categories_to_categories VALUES ( 3 ,3 , 1 );
INSERT INTO categories_to_categories VALUES ( 4 ,4 , 0 );
INSERT INTO categories_to_categories VALUES ( 5 ,5 , 4 );
INSERT INTO categories_to_categories VALUES ( 6 ,6 , 4 );
CREATE TABLE products(
product_id INT,
category_id INT,
name varchar(50),
sales int
);
INSERT INTO products VALUES ( 1 ,2 , 'black t-shirt', 600 );
INSERT INTO products VALUES ( 2 ,2 , 'blue t-shirt', 400 );
INSERT INTO products VALUES ( 3 ,3 , 'white tank-top', 100 );
INSERT INTO products VALUES ( 4 ,5 , 'red baseball cap', 50 );
INSERT INTO products VALUES ( 5 ,6 , 'yellow beanie', 50 );
查询1 :
select C.category_id, C.category,sum(sales) sales
from categories c
LEFT JOIN (
SELECT parent_category_id as 'category_id',sales
FROM categories_to_categories c1
INNER JOIN products p ON p.category_id = c1.category_id
UNION ALL
SELECT c1.category_id,sales
FROM categories_to_categories c1
INNER JOIN products p ON p.category_id = c1.category_id
) ctc on c.category_id = ctc.category_id
group by C.category_id, C.category
Results :
| category_id | category | sales |
|-------------|---------------|-------|
| 1 | shirts | 1100 |
| 2 | t-shirts | 1000 |
| 3 | tank-tops | 100 |
| 4 | hats | 100 |
| 5 | baseball caps | 50 |
| 6 | beanie | 50 |