当前位置:网站首页>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
如此便实现了需求所需。
拓展
两表联表查询具体可以分为七种,见网图如下:
边栏推荐
猜你喜欢
[Happy Qixi Festival] How does Nacos realize the service registration function?
学会反射后,我被录取了(干货)
Metasploit-域名上线隐藏IP
Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
如何写好测试用例
OpenCV:10特征检测
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
SQL关联表更新
子连接中的参数传递
随机推荐
仿网易云音乐小程序-uniapp
LeetCode Hot 100
MongoDB permission verification is turned on and mongoose database configuration
【数据挖掘概论】数据挖掘的简单描述
手写分布式配置中心(1)
线程三连鞭之“线程的状态”
LeetCode Hot 100
标识符、关键字、常量 和变量(C语言)
KT148A语音芯片怎么烧录语音进入芯片里面通过串口和电脑端的工具
ARC129E Yet Another Minimization 题解 【网络流笔记】
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions
工业物联网 —— 新型数据库的召唤
测试经理要不要做测试执行?
导入JankStats检测卡帧库遇到问题记录
Huggingface入门篇 II (QA)
如何写好测试用例
Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
[Cloud Native--Kubernetes] Pod Controller
SQL association table update