mysql - SQL - Left outer join with 2 tables and derived tables -
my scenario follows:
table persons - id (primary key) - name table logs - id (primary key) - person_id (foreign key) - datetime - status
now want create sql query in order retrieve:
- all names persons without relation table logs
- and relation table logs linked field id , person_id.
- only latest records person table logs (ie latest log id)
this query retrieves names matched relation:
select p.id, p.name, date_format(l.datetime,'%d-%m-%y %h:%i:%s') datetime, l.status persons p left outer join logs l on p.id = l.person_id l.id in (select id logs t1 inner join (select max(id) max_id logs group person_id ) t2 on t1.id = t2.max_id) order l.status, p.name";
how names without relation table logs well?
you can use left join:
select p.name persons p left join logs l on p.id = l.person_id l.id null
Comments
Post a Comment