简体   繁体   中英

SQL how to get informations in result set only latest date?

I have 2 tables, one to many, we'll call them a and b, in b many there's a date, I would like a result set like so:

select a.*, b.all the columns but only for the latest date
where a.id = b.foreign_key_to_a

how would you suggest to do it?

edit: I forgot to mention I expect a result set longer than one, so is for very record in a that it should check it's latest date

I don't know if I've understood your question correctly but you could try something like this:

SELECT a.*,b.* from a join b on a.pk=b.fk
where b.date = (select max(date) from date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM