通过另一个表格行选择行顺序

我们称这个表为CATEGORIES:

           +---------+-------
           | id  | name     | 
           +-----+----------|
           |   1 | CATEG 1  |
           |   2 | CATEG 2  |
           |   3 | CATEG 3  |
           |   4 | CATEG 4  |



           And this is Items:
           +----+-------------+---------------+--------
           | id |    name     |  categoryid   | price |
           +----+-------------+---------------+--------
           |  1 |  Book       |       2       |   5$  |
           |  3 |  Toy Car    |       1       |   8$  |
           |  2 |  Pencil     |       2       |   3$  |
           |  4 |  Toy Box    |       1       |   2$  |
           |  5 |  BookCase   |       2       |   4$  |
           |  6 |  Barbie     |       1       |   7$  |
           +----+-------------+---------------+-------+ 

如何从物品价格按表格顺序选择类别? 我需要两个查询吗?或者我可以使用连接语句?


像这样的东西可能会起作用

 select c.id, sum(price) category_price
 from categories c
 left join items i on c.id = i.categoryid
 group by c.id
 order by category_price
链接地址: http://www.djcxy.com/p/63807.html

上一篇: Selecting rows order by another table row

下一篇: MySQL Removing Value If Exists in Another Row