热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MySQL按X顺序然后分组按Y然后按字母顺序排列Z.-MySQLOrderByXthenGroupByYthenArrangeZAlphabatically

Iwouldliketo1)OrderbyPriceASC(showcheapest)then2)GroupByItemnamethen3)SortAlphab

I would like to 1) Order by Price ASC (show cheapest) then 2) Group By Item name then 3) Sort Alphabatically by Item name.

我想1)按价格ASC(显示最便宜)订购然后2)按项目名称分组然后3)按项目名称按字母顺序排序。

Here are 2 colums (will only include the 2 relevant columns, there are other colums like model, color etc..) enter image description here

这里有2个列(仅包含2个相关列,还有其他列如模型,颜色等)。

Results I want:

结果我想:

enter image description here

Thanks

1 个解决方案

#1


0  

SELECT Item, Min(Price), Other_Columns FROM table GROUP BY Item ORDER BY Item

IF you want the Id, color, and model that belong to the min price, things get complicated. You could use something like:

如果你想要属于最低价格的Id,颜色和模型,事情会变得复杂。你可以使用类似的东西:

SELECT * FROM `table` WHERE id IN 
    (SELECT MIN(id) FROM `table` T1 WHERE Price=
        (SELECT MIN(Price) FROM `table` T2 WHERE T1.Item=T2.Item) 
     GROUP BY Item, Price)

Basically what you do is:first establish the min price per Item. Than find all the first id's from every Item with that price. Than select all the columns from all the records with those Id's. Probably it can be done simpler, but this is easiest to understand.

基本上你所做的是:首先确定每件物品的最低价格。然后找到具有该价格的每个物品的所有第一个ID。然后选择具有这些Id的所有记录中的所有列。可能它可以更简单,但这是最容易理解的。

Iam not sure f that is what you want, since your question was not very specific/clear (hence all the negative votes). But I sort of came to believe it is.

我不确定那是你想要的,因为你的问题不是很具体/清楚(因此所有的反对票)。但我有点开始相信它。


推荐阅读
author-avatar
风之淡然1_925
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有