mysql - Joining three tables such that extra matches are discarded? -


how can write query give results of 3 tables such there's 1 result per "line"?

the tables are:

t1 (id, name, ip)  t2 (id, date_joined)  t3 (id, address, date_modified) 

the relations are: t1-t2 1:1, t1-t3 1:m - there can many address rows per id in t3.

what want listing of users fields above, if have address, want record 1 (bonus if latest 1 based on t3.date_modified).

so should end number of records in t1 (happens equal t2 in case) , no more.

i tried:

select t.id, t.name, t.ip, tt.id, tt.date_joined, ttt.id, ttt.address  t1 t join t2 tt on (t.id = tt.id) join t3 ttt on (t.id = ttt.id) 

and every sensible combination of left, right, inner, etc joins think of! keep getting multiple duplicate because of t3

this query should work:

select      t1.id, t1.name, t1.ip, t2.date_joined, t3x.address t1 join t2 on t1.id = t2.id left join (     select t3.*      t3      join (        select id, max(date_modified) max_date         t3         group id     ) max_t3 on t3.id = max_t3.id , t3.date_modified = max_t3.max_date ) t3x on t1.id = t3x.id 

first normal join between t1 , t2 , left join derived table (t3x) set of t3 rows having latest date.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -