当前位置:网站首页>MySQL 多表关联一对多查询实现取最新一条数据
MySQL 多表关联一对多查询实现取最新一条数据
2022-07-30 20:15:00 【m0_66557301】
场景:个人用户进行实名认证。一个人存在多个实名认证结果。期望获取到每个人最新一条实名认证结果 加 个人信息。
一、写出查询用户信息加实名认证得sql
SELECT
*
FROM
USERS u
LEFT JOIN USER_AUTH ua ON ua.userId = u.id
二、改造sql 可以获取到最新一条实名记录
方法一:
1、大致思路进行 left join得这张表提前 先分组筛选出 最新一条记录 。
SELECT
*
FROM
USERS u
LEFT JOIN ( SELECT MAX( id ) AS id, userId FROM USER_AUTH GROUP BY userId ) AS ua ON ua.userId = u.id
方法二:
先联表后,然后取出出第一个
SELECT
*
FROM
(
SELECT
u.id,
row_number() over ( PARTITION BY u.id ORDER BY u.id ) AS rowno
FROM
USERS u
LEFT JOIN USER_AUTH ua ON u.id = ua.userId
) tmp
WHERE
rowno = 1;
方法三:
SELECT
u.id
FROM
USERS u
LEFT 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 需要缓存下面写法
区别在于 子查询得order by 需要 使用 top
SELECT
u.id
FROM
USERS u
LEFT 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 上诉方法 mysql 运行结果:

Navicat Premium 15 mysql 执行结果

Navicat Premium 15 SQL server 执行结果


emmmmm
sql 多次执行发现时间 都不稳定。 综合下来 方法一 得效率更好
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

推荐系统:AB测试(AB Test)

【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
![Recommendation System - Sorting Layer: Sorting Layer Architecture [User and Item Feature Processing Steps]](/img/e7/4000f9f1f091970442ae3ecbc34c9f.png)
Recommendation System - Sorting Layer: Sorting Layer Architecture [User and Item Feature Processing Steps]

从离线到实时对客,湖仓一体释放全量数据价值

MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
![[PM only] Quickly count who else in the team has not registered and reported information, and quickly screen out the members of their own project team who have not completed the list of XXX work items](/img/74/94c254b7694a77d635f5f538a72fe0.png)
[PM only] Quickly count who else in the team has not registered and reported information, and quickly screen out the members of their own project team who have not completed the list of XXX work items

Face-based Common Expression Recognition (2) - Data Acquisition and Arrangement

ceph的部署练习

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案

PPT如何开启演讲者模式?PPT开启演讲者模式的方法
随机推荐
网络安全实验环境搭建
【无标题】多集嵌套集合使不再有MultipleBagFetchException
[PM only] Quickly count who else in the team has not registered and reported information, and quickly screen out the members of their own project team who have not completed the list of XXX work items
推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】
基于人脸的常见表情识别(2)——数据获取与整理
vlookup函数匹配不出来的原因及解决方法
KEIL problem: [keil Error: failed to execute 'C:\Keil\ARM\ARMCC']
Recommendation System - Sorting Layer: Sorting Layer Architecture [User and Item Feature Processing Steps]
Flex布局详解
MySQL database master-slave configuration
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
MySQL_关于JSON数据的查询
Multi-threaded mutex application RAII mechanism
银行数据资产转换能力弱?思迈特软件助力解决银行困境
倾斜文档扫描与字符识别(opencv,坐标变换分析)
Mapped Statements collection does not contain value for的解决方法
18.客户端会话技术Cookie
.eslintrc.js for musicApp
ELK log analysis system
TensorFlow2: Overview