mysql - Selecting row with max date from multiple almost identical tables -
if can retrieve recent name each id table in mysql database so:
select n.id, n.name, n.date $table n inner join (select id, max(date) date $table group id) max using (id, date); how retrieve recent name 3 identical tables (call them $table, $table2, $table3)? share same column structure , id found 1 table may or may not present in other two. think of 1 large table split 3 (but 2 of them containing 2 columns irrelevant in instance). union best solution? if so, there way without mile-long query?
constraint:
id not auto-incrementing unique integer unfortunately
you can use union all. 1 slight simplification group_concat()/substring_index() trick:
select id, max(date) date, substring_index(group_concat(name order date desc), ',', '') mostrecentname (select t.* $table1 t union select t.* $table2 t union select t.* $table3 ) t group id; this make assumptions. name cannot contain , (although easy enough change separator. in addition, intermediate result group_concat() cannot exceed threshold (which determined user-settable system parameter).
Comments
Post a Comment