当前位置:网站首页>gorm联表查询-实战
gorm联表查询-实战
2022-08-05 00:11:00 【景行三千】
gorm联表查询-实战
缘起
- 做一个政府项目遇到的一个项目需求所需要的技术
- 需求背景大概是这样:有一张
workers表,一张worker_records表,工作人员的信息记录在workers表,同时当工作人员上班时候会打卡,而打卡记录就在这个worker_records表
| workers表部分需要用到字段如下 | worker_records表部分需要用到字段如下 |
|---|---|
| gzryxm——意为:工作人员姓名 | gzrxm——意为:工作人姓名 |
| gzrysjh——意为:工作人员手机号 | gzrsjh——意为:工作人手机号 |
| gzrysfz——意为:工作人员身份证 | gzrsfz——意为:工作人身份证 |
身份证号唯一
需求:拿到未打卡人的姓名,手机号,身份证号
画出图就是以下这样

gorm实现
db.Model(&Worker{
}).
Select("workers.csbh,workers.gzryxm,workers.gzrysjh,workers.gzrysfz").
Joins("left join worker_records on workers.gzrysfz = worker_records.gzrsfz").
Where("worker_records.gzrsfz", nil).
Scan(¬ClockedInPeopleAll)
MySQL原生实现
SELECT w.gzryxm,w.gzrysjh,w.gzrysfz
FROM workers w
LEFT JOIN worker_records wr
ON w.gzrysfz = wr.gzrsfz
WHERE wr.gzrsfz IS NULL
如此便实现了需求所需。
拓展
两表联表查询具体可以分为七种,见网图如下:

边栏推荐
- Mysql_13 事务
- LeetCode Hot 100
- Senior game modelers tell newbies, what are the necessary software for game scene modelers?
- 子连接中的参数传递
- KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明
- 中日颜色风格
- VMware NSX 4.0 -- 网络安全虚拟化平台
- 【云原生--Kubernetes】调度约束
- [CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1
- Uniapp dynamic sliding navigation effect demo (finishing)
猜你喜欢
随机推荐
SQL关联表更新
Ab3d.PowerToys and Ab3d.DXEngine Crack
软件质量评估的通用模型
oracle创建用户
Couple Holding Hands [Greedy & Abstract]
【unity编译器扩展之模型动画拷贝】
翁恺C语言程序设计网课笔记合集
[Cloud Native--Kubernetes] Pod Controller
uniapp sharing function - share to friends group chat circle of friends effect (sorting)
机器学习(公式推导与代码实现)--sklearn机器学习库
The master teaches you the 3D real-time character production process, the game modeling process sharing
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
~ hand AHB - APB Bridge 】 【 AMBA AHB bus
#yyds dry goods inventory #Switching equipment serious packet loss troubleshooting
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
电赛必备技能___定时ADC+DMA+串口通信
【LeetCode】双指针题解汇总
Modelers experience sharing: model study method
.net(C#)获取两个日期间隔的年月日
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?


![[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1](/img/8b/360df9a9094037dc358cb21c60cdc8.png)





