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


i have 2 tables fetch values both tables, situation first table contains records second table may or maynot.

here 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 want is,(only 1 record second table)

id | rank | swiped_on 1 | tl | 21-4-14 2 | pl | ------- 3 | mg | 25-4-14

please help

you can use left join subselect table tab2

select t.*,t1.swiped_on tab1 t left join (select id , max(swiped_on) swiped_on           tab2 group id           ) t1 on(t.id = t1.id) 

fiddle demo

or max of swiped_on tab2

select t.*,max(t1.swiped_on) swiped_on tab1 t left join tab2 t1 on(t.id = t1.id) group t.id 

fiddle demo


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -