选择distinct并按多个smallint列排序

时间:2017-05-05 06:08:35

标签: php mysql

widthheightsmallint(6) 我想从distinctwidth订购width asc值,然后按height asc订购。 这是我的尝试:

- $sql = "select * from banners group by width order by width, height asc";  
- $sql = "select * from banners group by width order by width asc, height asc";  
- $sql = "select * from banners group by width order by width asc, height";  

没有任何作用。选择按宽度排序,但高度不合适。

1 个答案:

答案 0 :(得分:1)

使用group by而不是distinct是不合适的(并且不允许启动mysql 5.7)只选择一行没有显式名称

您应该在选择和聚合函数中使用显式列名,列名不在

组中

通过这种方式,您可以真正控制所选值和行中结果的顺序

例如:

select distinct width,  height 
from banners 
order by width, height asc

select  width,  max(height )
from banners 
group by width
order by width, height asc