MySQL JOIN the most recent row only?(MySQL 仅加入最近的一行?)
问题描述
我有一个表 customer,其中存储了 customer_id、电子邮件和参考.还有一个额外的表 customer_data 存储对客户所做更改的历史记录,即当有更改时插入新行.
I have a table customer that stores a customer_id, email and reference. There is an additional table customer_data that stores a historical record of the changes made to the customer, i.e. when there's a change made a new row is inserted.
为了在一个表中显示客户信息,需要将两个表连接起来,但是只有 customer_data 中最近的一行才应该连接到客户表中.
In order to display the customer information in a table, the two tables need to be joined, however only the most recent row from customer_data should be joined to the customer table.
它变得有点复杂,因为查询是分页的,所以有一个限制和一个偏移量.
It gets a little more complicated in that the query is paginated, so has a limit and an offset.
我怎样才能用 MySQL 做到这一点?我想我想在某个地方放一个 DISTINCT...
How can I do this with MySQL? I think I'm wanting to put a DISTINCT in there somewhere...
当时的查询是这样的-
此外,我认为我可以以这种方式将 CONCAT 与 LIKE 一起使用是否正确?
Additionaly, am I right in thinking I can use CONCAT with LIKE in this way?
(我很欣赏 INNER JOIN 可能是错误的 JOIN 类型.我实际上不知道不同 JOIN 之间有什么区别.我现在要研究一下!)
(I appreciate that INNER JOIN might be the wrong type of JOIN to use. I actually have no clue what the difference is between the different JOINs. I'm going to look into that now!)
推荐答案
您可能想尝试以下操作:
You may want to try the following:
请注意,JOIN
只是 INNER JOIN
的同义词.
Note that a JOIN
is just a synonym for INNER JOIN
.
测试用例:
结果(没有LIMIT
和WHERE
的查询):
Result (query without the LIMIT
and WHERE
):
这篇关于MySQL 仅加入最近的一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!