当前位置:网站首页>mysql中一对多关联,获取多表中最新一条数据
mysql中一对多关联,获取多表中最新一条数据
2022-06-27 19:53:00 【喵喵喵更多】
背景
在msyql查询中经常会出现一对多查询,只查询多表中的其中一条最新的数据或者达成某个条件的数据。如果只用GROUP BY已经不能满足查询结果,这时查询就会变得复杂。
举个例子,学校表、课程表、课程排序表,我需要每个学校下面课程排序最靠前的一个课程。
在有些案列中我们可以看到被人使用的方法是:
``SELECT c.Id FROMt_course_rank AS ocrLEFT JOIN ( SELECT * FROM t_course WHERE coursetype = 2 ORDER BY Visit DESC ) AS c ON c.Id = ocr.courseidLEFT JOIN t_school AS s ON c.LoginId = s.LoginIdWHEREc.LoginId IN (19) GROUP BY c.LoginId
通过临时表关联完成某中条件中最新数据,但是这种查询不准确,无法满足条件。
解决方案
1.先查出所有数据,然后遍历去查询(php中foreach中查询),简单,但是效率很低,过程可能复杂。
2. 先排序然后在调用
边栏推荐
- DCC888 :Register Allocation
- Crawler notes (1) - urllib
- It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
- 《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题
- Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
- Use Fiddler to simulate weak network test (2g/3g)
- 北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
- 爬虫笔记(2)- 解析
- 从学生到工程师的蜕变之路
- 管理系統-ITclub(下)
猜你喜欢

信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可

It smells good. Since I used Charles, Fiddler has been completely uninstalled by me

How to participate in openharmony code contribution

Go from introduction to practice - error mechanism (note)

Codeforces Round #716 (Div. 2)

Management system itclub (Part 1)

爬虫笔记(3)-selenium和requests

结构化机器学习项目(一)- 机器学习策略

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

从学生到工程师的蜕变之路
随机推荐
xpath
同花顺炒股软件可靠吗??安全嘛?
管理系统-ITclub(上)
Go from introduction to actual combat - execute only once (note)
Yolov6: the fast and accurate target detection framework is open source
Go from introduction to actual combat - task cancellation (note)
Go from introduction to practice - error mechanism (note)
Summary of Web testing and app testing by bat testing experts
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
Conversion between flat array and JSON tree
Read write separation master-slave replication of MySQL
《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题
Management system itclub (medium)
Start the start php
Go from introduction to actual combat - only any task is required to complete (notes)
CDH集群之YARN性能调优
Gbase 8A OLAP analysis function cume_ Example of dist
Record a list object traversal and determine the size of the float type
宏任务、微任务理解
爬虫笔记(2)- 解析