我记不起 php 中对相关表中的数据进行排序的正确方法了?我不确定它是否可以在一个查询中实现?
table brands
id name
------------
1 Disney
2 Pepsi
3 Sony
table products
id name brandId
-----------------------
1 cd-playerX 3
2 nice poster 1
3 usb-radio 3
4 cd-playerY 3
我想列出按品牌表(order=asc)的名称排序的所有产品,如下所示:
- 漂亮的海报(迪士尼)
- cd-playerX(索尼)
- cd-playerY(索尼)
- USB radio (索尼)
有人吗?
最佳答案
select p.name + ' (' + b.name + ')' as fullName
from products as p
left join brands as b
on p.id = b.brandId
order by b.name asc, p.name asc --optional for brands with multiple products
关于sql-order-by - 与mysql中的相关表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10786849/