简体   繁体   中英

select distinct and order by multiple smallint columns

Columns width and height are smallint(6) .
I want to select distinct values from width order by width asc and then order by height asc . 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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