Home > Database > Mysql Tutorial > mysql表关联只取关联表中最近一条数据

mysql表关联只取关联表中最近一条数据

WBOY
Release: 2016-06-06 09:32:55
Original
1931 people have browsed it

mysql

表A{id,userId,logTime}
表B{id,userId,departmentId,updateTime}

表A为数据记录
表B相当于历史记录表(userId在updateTime时间之前的departmentId)

现在表A与表B关联 但是只能让表B中符合条件的最近一记录关联上
(也是updateTime在logTime之前并且最近的一条)
我是这么做的
SELECT A.id,B.userId,B.departmentId FROM A INNER JOIN B
ON A.userId=B.userId AND A.logTime>B.updateTime
但是这样会将B表中多条符合条件的数据全取出来,而我只想要最近的一条就足够了
表A表B中都可能有多个userId与日期!!
求解

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template