Selecting rows order by another table row
Let's call this table 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$ |
+----+-------------+---------------+-------+
How Do I select categories from table order by items price ? Will I need two queries for this or I could use a join statement?
像这样的东西可能会起作用
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/63808.html
下一篇: 通过另一个表格行选择行顺序