[英]select distinct and order by multiple smallint columns
列的width
和height
为smallint(6)
。
我想按width asc
从width
顺序中选择distinct
值,然后按height asc
从顺序中选择distinct
值。 这是我的尝试:
- $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";
什么都没有。 选择的顺序按宽度很好,但是按高度顺序不正确。
使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.