如何从连接表中获取最新数据?

时间:2021-02-08 10:49:11

标签: mysql

我有 2 张表要注册。第一个是注册有id_registrationentry_date。第二个是历史,有id_historyid_registrationentry_date

TABLE REGISTRATION

id_registration | entry_date
----------------------------
1               | 2021-02-07
2               | 2021-02-08
----------------------------


TABLE HISTORY

id_history | id_registration | entry_date
-----------------------------------------
1          | 1               | 2021-02-07
2          | 2               | 2021-02-08
3          | 1               | 2021-02-08
-----------------------------------------

在历史表中,有两行具有相同的 id_registration。所以,我想从 entry_date 获取最新的引用。这是我的查询

SELECT r.id_registration
     , h.id_history
     , h.entry_date 
  FROM registration AS r 
  LEFT 
  JOIN history AS h 
    ON h.id_registration = r.id_registration
 GROUP 
    BY h.id_registration
ORDER BY h.entry_date DESC

但是这个查询的结果是:

id_registration | id_history | entry_date
-----------------------------------------
2               | 2          | 2021-02-08    
1               | 1          | 2021-02-07
-----------------------------------------

我期望的是:

id_registration | id_history | entry_date
-----------------------------------------
1               | 3          | 2021-02-08
2               | 2          | 2021-02-08
-----------------------------------------

我的查询有什么错误?

0 个答案:

没有答案
相关问题