当前位置:网站首页>MySQL multi-table association one-to-many query to get the latest data
MySQL multi-table association one-to-many query to get the latest data
2022-08-02 06:04:00 【good moon births autumn】
Scenario: Individual users perform real-name authentication.There are multiple real-name authentication results for one person.Expect to get the latest real-name authentication result plus personal information for each person.
1. Write the sql for querying user information and real-name authentication
SELECT*FROMUSERS uLEFT JOIN USER_AUTH ua ON ua.userId = u.idSecond, the latest real-name record can be obtained by transforming sql
Method one:
1. The general idea is to perform a left join to obtain this table in advance and filter out the latest record by grouping.
SELECT*FROMUSERS uLEFT JOIN ( SELECT MAX( id ) AS id, userId FROM USER_AUTH GROUP BY userId ) AS ua ON ua.userId = u.idMethod 2:
Connect the table first, then take out the first one
SELECT*FROM(SELECTu.id,row_number() over ( PARTITION BY u.id ORDER BY u.id ) AS rownoFROMUSERS uLEFT JOIN USER_AUTH ua ON u.id = ua.userId) tmpWHERErowno = 1;Method three:
SELECTu.idFROMUSERS uLEFT JOIN USER_AUTH ua ON ua.id = (SELECT b.id FROM USER_AUTH b WHERE b.userId = u.id HAVING 1 ORDER BY b.id )sql server needs to cache the following wording
The difference is that subquery order by needs to use top
SELECTu.idFROMUSERS uLEFT JOIN USER_AUTH ua ON ua.id = (SELECT top 1 b.id FROM USER_AUTH b WHERE b.userId = u.id ORDER BY b.id )DataGrip Appeal method mysql results:

Navicat Premium 15 mysql execution result

Navicat Premium 15 SQL server execution result


emmmmm
sql is executed multiple times and found that the time is not stable.Taken together Method 1 is more efficient
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢

ES6——class类实现继承

MySQL 灵魂 16 问,你能撑到第几问?

Does Conway's Law Matter for System Architecture?

MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)

How to quickly delete the compressed package password?

12个MySQL慢查询的原因分析

从头开始实现YOLOV3

The line chart with square PyQt5_pyqtgraph mouse
Deep Blue Academy - Handwritten VIO Homework - Chapter 2

MobaXsterm如何使用
随机推荐
Crawler_crawl wasde monthly supply and demand balance table (example)
MySQL如何创建用户
【无标题】
通关剑指 Offer——剑指 Offer II 008. 和大于等于 target 的最短子数组
300M级mysql数据库跨版本迁移流程
MES如何做好生产过程监控,本文给出了详细解答
12个MySQL慢查询的原因分析
MYSQL 唯一约束
从DES走到AES(现代密码的传奇之路)
Jmeter使用多线程测试web接口
2022年7月学习计划完成情况
UE4 蓝图实现AI随机移动
mysql 存储过程详解
递归实现指数型枚举(DAY 91)
在 .NET MAUI 中如何更好地自定义控件
【云原生】什么是CI/CD? | CI/CD 带来的好处
翻转(DAY 97)
MES系统物料管理的五大功能,建议收藏
系统层面知识连接收藏
力扣练习——44 路径总和 III