Rowan San
Rowan San

Reputation: 1577

joining two tables, select values from two with second table having no records

I have two tables where i fetch values from both tables, the situation is the first table always contains records but second table may or maynot.

here are tables

TAB1
id | rank 1 | TL 2 | PL 3 | MG
TAB2
num | id | swiped_on 1 | 1 | 20-4-14 2 | 1 | 21-4-14 3 | 3 | 25-4-14

the result i want is,(only one record from the second table)

id | rank | swiped_on 1 | TL | 21-4-14 2 | PL | ------- 3 | MG | 25-4-14

please help

Upvotes: 0

Views: 79

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

You can use a left join with a subselect from table tab2

select t.*,t1.swiped_on
from TAB1 t
left join (select id , max(swiped_on) swiped_on
          from TAB2 group by id
          ) t1
on(t.id = t1.id)

Fiddle demo

Or just get the max of swiped_on from tab2

select t.*,max(t1.swiped_on) swiped_on
from TAB1 t
left join TAB2 t1
on(t.id = t1.id)
group by t.id

Fiddle demo

Upvotes: 1

Related Questions