sql mysql Max() -


i need display query max statement don't know how. explain 2 simples tables understand better.

  • 1 table family
 family ------ id int(1) - pk name varchar(255) 
  • 1 table person
 person ------ id int(1) - pk fk_id_family - fk family(id) first_name varchar(255) age int(1) 

content of tables

 family ------ 1 - dupont 2 - martin 3 - petit 4 - dupres 
 person ------ 1 - 1 - jean - 70 (family dupont) 2 - 1 - jeannette - 65 (family dupont) 3 - 1 - pierre - 35 (family dupont) 4 - 1 - nicolas - 29 (family dupont) 5 - 2 - andre - 69 (family martin) 6 - 2 - ginette - 58 (family martin) 7 - 2 - benjamin - 25 (family martin) 

i need have query display family.name, person.first_name, person.age person older in family , family name there not person linked to. result one.

 dupont - jean - 70 martin - andre - 69 petit - null - null dupres - null - null 

thank help

first need maximum age grouping fk_id_family in person table , join family. way, name , highest age together.

now, need make join person table first_name column. have not tested below sql. but, hope, give expected output.

select  f.name, pr.first_name, p.age family f  inner join (select fk_id_family id, max(age) age person group fk_id_family) p on p.id=f.id inner join  person pr on pr.fk_id_family=p.id 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -