简体   繁体   English

通过多个smallint列选择不同的顺序

[英]select distinct and order by multiple smallint columns

Columns width and height are smallint(6) . 列的widthheightsmallint(6)
I want to select distinct values from width order by width asc and then order by height asc . 我想按width ascwidth顺序中选择distinct值,然后按height asc从顺序中选择distinct值。 Here is my try: 这是我的尝试:

- $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";  

Nothing works. 什么都没有。 Selection is ordered fine by width, but not properly by height. 选择的顺序按宽度很好,但是按高度顺序不正确。

the use of group by instead of distinct is no proper (and starting form mysql 5.7 is not allowed ) for select only one row without explicit name 使用group by而不是distinct是不合适的(并且不允许以mysql 5.7开头的形式)仅选择没有显式名称的一行

You should use explicit column name in select and aggregation function for column name not in group by 您应在选择和汇总功能中使用显式列名,以使列名不在分组依据中

in this way you really control the selected value and the order of the resultin rows 这样,您就可以真正控制所选值和结果在行中的顺序

eg: 例如:

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM