我有两个表用于产品,另一个表是类别
我有一个包含category和subcategory字段的表。我想要特定类别下不同子类别的总和。像表
cat_id| category |
------+--------------+--
1 | Cloth |
2 | Food |
3 | Fuel |
4 | School |
另一个是产品表
id| name | cat_id |
------+-----------+---------+--
1 | skirt | 1
2 | shirt | 1
3 | St Xavier| 4
4 | Petrol | 3
现在我希望结果
id | name | cat_name |
------+-----------+---------+--
1 | skirt | Cloth
2 | shirt | Cloth
3 | St Xavier| School
4 | Petrol | School
我的Sql查询是什么,请帮助!!!
答案 0 :(得分:6)
SELECT p.`id` AS "id" , p.`name`AS "Product Name",c.`category`AS "Category"
FROM `product` p
INNER JOIN `category`c ON (p.`cat_id`=c.`cat_id`)
ORDER BY p.`id` ASC
答案 1 :(得分:1)
试试这个
SELECT A.id, A.name, B.category AS cat_name
FROM product A, category B
WHERE A.cat_id=B.cat_id