解决distinct与order by 的冲突
解决 by 冲突 order Distinct
2023-09-11 14:17:21 时间
sql="select distinct id from test order by otherfield desc"
需要找到不同的id,同时又想让记录按fbsj排序。但是这样一定会出现错误,出现distinct与order by冲突问题。如何来得到完美的解决呢,看下面的方法,依然用一条sql语句
sql="select id from test group by id order by max(<span style="font-family: Arial, Helvetica, sans-serif;">otherfield</span>) desc"
用group by 代替disitnct实现找不同id的功能。
以上已经过测试,可以完美解决distinct与order by的冲突问题
原载: 大连网站制作 http://www.noonenet.cn/
----------------------
补充两句在Django中解决这个问题是,直接annotate max即可,annotate本身自动添加group by
这个问题的解决体现了对SQL集合理论的理解,第一种方式其逻辑在集合理论中出现了跳跃,所以无法完成。
相关文章
- 镜像推送时出现 server gave HTTP response to HTTPS client 问题的解决方法
- 解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat
- 解决sql_mode=only_full_group_by的问题
- Caused by: org.apache.ibatis.binding.BindingException: Parameter 'parameter' not found.解决
- 架构设计:负载均衡层设计方案(1)——负载场景和解决方式
- 解决which is not functionally dependent on columns in GROUP BY clause;...sql_mode=only_full_group_by
- 解决union查询order by 排序失效的问题
- [转]mysql问题解决SELECT list is not in GROUP BY clause and contains nonaggregated column
- 解决“指定的服务已经标记为删除”问题
- 解决 aliyun-oss-spring-boot-starter 无法导入的问题
- Kali Linux 2017中Scapy运行bug解决
- 【mysql问题】完美解决this is incompatible with sql_mode=only_full_group_by
- Laya引擎Dialog遮罩层无法移除如何解决
- 解决最新Java12 安装
- Mysql Order By 字符串排序,mysql 字符串order by
- mysql不支持emoji表情的问题的解决方法
- Java学习-083-Idea提示:You aren‘t using a compiler supported by lombok, so lombok will not work and has been disabled 问题解决
- 【SpingSecurity】解决lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null“
- 浅析部署遇到的2个报错:Caused by: java.net.SocketTimeoutException: connect timed out的原因及解决、no suitable node (host-mode port already in use on 1 node)原因及解决
- 解决Maven报Plugin execution not covered by lifecycle configuration
- [已解决]ValueError: row index was 65536, not allowed by .xls format
- curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection reset by peer解决
- IIS-真正解决ASP.NET每一个页面首次访问超级慢的问题
- 远程连接数据库 出现 Client does not support authentication protocol requested by server的解决方法
- SpringBlade IDEA常见问题解决
- 圣诞特辑-解决idea新建报错The required Java version 11 is not supported by the project SDK 1.8.