[英]How to SELECT records from One table If Matching Record In Not Found In Other Table
[英]how to select one record from multi records of another table?
我有多個表,表1:
ID Name
1 Adam
2 Bob
...
表2:
ID Visit
1 2019-01-01
2 2019-01-02
1 2019-01-03
1 2019-01-04
2 2019-01-04
1 2019-01-05
...
我想從表2獲取表1中每個名稱的最新訪問,例如
Name LastVisit
Adam 2019-01-05
Bob 2019-01-04
我嘗試使用
select tbl1.name as "Name", MAX(tbl2.visit) as LastVisit
from table1 tbl1 join table2 tbl2 on tbl1.id=tbl2.id;
但它不起作用,我該怎么辦?
您只需要一個group by
:
select t1.name, MAX(t2.visit) as LastVisit
from table1 t1 join
table2 t2
on t1.id = t2.id
group by t1.name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.