sql-order-by - 与mysql中的相关表进行排序

标签 sql-order-by

我记不起 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/

相关文章:

c# - Linq to Objects 按任意数量的参数排序

MySQL 按组对表进行排序

string - Prolog 中的字符串排序

mysql - ADO.NET linq 到 MYSQL,orderby 不工作

mysql - SQL ORDER BY 查询

使用 FORMAT 函数时 MySQL 排序

mysql - sql 选择如果和排序依据

linq - LINQ 中的 OrderBy 如何工作(在幕后)?

SQLite ORDER BY 时间戳没有给出预期结果

sqlite - 按深度和兄弟顺序对记录进行排序