当前位置:网站首页>一些场景下基于MySQL比较好的实现思路(持续更新)
一些场景下基于MySQL比较好的实现思路(持续更新)
2022-04-22 19:13:00 【atwdy】
Q:根据部分列字段去重,返回去重后结果的全部字段
去重很容易想到distinct,但在这里并不适用,因为distinct的返回结果只包含被distinct的字段,不能同时满足即对部分字段distinct又要返回的结果包含全部字段。
思路1:分组+聚合,根据去重字段分组,对非去重字段进行max或min聚合。
思路2:select * from 表名 where 主键 in (select max(主键) from 表名 group by 要去重的字段 )
第二种思路的好处在于 对于除去重字段外其它字段并不完全相等的记录,保留的字段都属于同一条记录中的字段。
Q:将查询出来的列值(一个或多个)拼接成一个字符串
使用mysql内置函数group_concat()
功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。(没有group by的话那么这个表的整体或者where过滤出来的部分整体就是一个分组)
语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator ‘分隔符’] )
说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。
eg:select group_concat(distinct name order by age separator "_") from t1 group by id having id = 2;
版权声明
本文为[atwdy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/atwdy/article/details/124047187
边栏推荐
- k9. Chapter 2: install kubernetes v1.0 based on binary package 20 -- basic environment configuration and kernel configuration (I)
- Can fire doors apply for BS 476-21 fire resistance test?
- 1372: Xiao Ming's bill
- ArrayList学习笔记
- 校园跑腿平台如何搭建?
- [Luogu] p2372 yyy2015c01 challenge perimeter (BFS)
- Revit (4) - 二开 -创建批量创建柱子
- Static dispatch and dynamic dispatch
- LeetCode 41. 缺失的第一个正数
- Introduction to feign, a microservice invocation component
猜你喜欢

Type of Flink window

【洛谷】P2372 yyy2015c01挑战算周长(BFS)

Learn this framework by using the mastering ABP framework

Can fire doors apply for BS 476-21 fire resistance test?

. net core minimal API upload file

Index in MySQL database (including SQL statement)

VS 2022 安装vld内存泄漏检测工具

(三)MySQL约束

ArrayList learning notes

LeetCode_343 整数拆分
随机推荐
Misuse of redis cache string
14-Sprak设置自动分区
MySQL index
The research group of Shenzhen University issued the evaluation report on sustainable development of Shenzhen (2016-2021)
【Appium踩坑】Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded
Detailed explanation of transfer method
微服务调用组件Feign介绍
Learn this framework by using the mastering ABP framework
MCU infrared module knowledge sharing theory is the basis of practical combat in the future
linux系统下redis相关问题
2019-11-19 solve the problem that go test executes a single test file and prompts undefined
06. 重构-简化条件表达式
Introduction to Alibaba micro service component Sentinel
Why can't async be used directly in useeffect
Can fire doors apply for BS 476-21 fire resistance test?
transfer方法详解
项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(三、Unity PlasticSCM多人协同)
Index in MySQL database (including SQL statement)
Bluetooth shield / cervical massage instrument / mini charger / probe thermometer, segment code LCD display driver ic-vk1088b qfn32 4 * 4 ultra small volume, 22seg * 4com, and can enter the power savi
2019-12-07 wav audio cutting and merging