Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择distinct并按多个smallint列排序_Php_Mysql - Fatal编程技术网

Php 选择distinct并按多个smallint列排序

Php 选择distinct并按多个smallint列排序,php,mysql,Php,Mysql,列width和height是smallint(6) 我想从width按width asc排序中选择distinct值,然后按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"; - $sq

width
height
smallint(6)

我想从
width
width asc
排序中选择
distinct
值,然后按
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";  

什么都不管用。选择按宽度排列,但按高度排列不正确。

使用group by而不是distinct(不允许从mysql 5.7开始)只选择一行,没有明确的名称

对于不在group by中的列名,应在选择和聚合函数中使用显式列名

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

例如:


您可以显示一些示例数据吗?以及预期的输出。这可能会帮助您了解为什么
按宽度分组
?@前进,因为我只需要与
宽度
不同的值。
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