Mysql 如何显示4个表中的数据?
我试图用mysql从4个表中获取数据,但我列出了下面所有4个表和列Mysql 如何显示4个表中的数据?,mysql,opencart,Mysql,Opencart,我试图用mysql从4个表中获取数据,但我列出了下面所有4个表和列 table1 = order_product Columns = order_product_id, order_id, product_id, name, price, total, tax, quantity table2 = product_to_category Columns - product_id, category_id table3 = category_description Column
table1 = order_product
Columns = order_product_id, order_id, product_id, name, price, total, tax, quantity
table2 = product_to_category
Columns - product_id, category_id
table3 = category_description
Columns - category_id, name
table4 = category
Columns - category_id, parent_id
我试图显示表3中的列名以及
表1和以下4个类别中的每一个类别中的“已售出商品的总数量”列和“总金额”列。
主要类别为类别_id=177,但
这有三个子类别,类别_id=191192193
所有3个都使用表4中的父类=177链接到主类?
我希望我能更好地解释这一点,并提供足够的详细信息和thx以获得任何帮助有更巧妙的方法来表达这一点,但我认为这个查询对您有用
SELECT t3.name, t2.category_id, sum(t1.quantity), sum(t1.total)
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON t1.product_id = t2.product_id
LEFT OUTER JOIN table3 t3 ON t2.category_id = t3.category_id
WHERE t2.category_id = 177
OR t2.category_id IN (SELECT t4.parent_id FROM table4 t4 WHERE t4.category_id = 177)
GROUP BY t3.name, t2.category_id
了解。感谢您的帮助,但这只显示了1行&我显示的是4行。显示主要类别名称的顶行id=177为类别描述=名称,以及类别的总计数量和总计id=191+192+193,然后显示类别描述=名称191192和193以及每个类别的总计数量和总计id=191,192和193使用类别表中的父类别id=177链接到主类别id=177