当前位置:网站首页>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. 先排序然后在调用
边栏推荐
- 渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
- Which method is called for OSS upload
- Use Fiddler to simulate weak network test (2g/3g)
- DCC888 :Register Allocation
- Codeforces Round #722 (Div. 2)
- Go from introduction to actual combat - execute only once (note)
- 深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
- [MySQL practice] query statement demonstration
- Codeforces Round #717 (Div. 2)
- Fill in the blank of rich text test
猜你喜欢

crontab定时任务常用命令

渗透学习-靶场篇-dvwa靶场详细攻略(持续更新中-目前只更新sql注入部分)

Infiltration learning - problems encountered during SQL injection - explanation of sort=left (version(), 1) - understanding of order by followed by string

Go from introduction to practice -- definition and implementation of behavior (notes)

登录凭证(cookie+session和Token令牌)

Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting

Codeforces Round #719 (Div. 3)

Vue+mysql login registration case

Character interception triplets of data warehouse: substrb, substr, substring

九九乘法表——C语言
随机推荐
Record a list object traversal and determine the size of the float type
Start the start php
Conversion between flat array and JSON tree
Crontab scheduled task common commands
AQS SOS AQS with me
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
结构化机器学习项目(二)- 机器学习策略(2)
渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解
xpath
Process judgment - ternary operation - for loop
Flask application case
Go from introduction to practice -- definition and implementation of behavior (notes)
Bean paste green protects your eyes
Crawler notes (1) - urllib
Vue+mysql login registration case
[microservices] (16) -- distributed transaction Seata
管理系统-ITclub(中)
How to participate in openharmony code contribution
Memoirs of actual combat: breaking the border from webshell
Fill in the blank of rich text test