php - MySql Query with INNER JOIN, GROUP BY and Max -
i have 2 tables, 1 looks this:
id, datetime, user_id, location , status // rest not relevant
and other looks this:
id, lastname // rest not relevant
now want entry of first table highest datetime
per user_id
, ask other table lastname
of user_id
. simple...
i tried way (whick looks promising false nontheless):
select w.datetime, w.user_id, w.status, e.lastname worktimes w inner join employees e on w.user_id=e.id right join (select max(datetime) datetime, user_id worktimes datetime>1467583200 , location='16' group user_id order datetime desc ) v on v.user_id=w.user_id group w.user_id order e.nachname;
could give me hint please? i'm stuck @ while , begin knots in brain... :(
you close, actually:
select w.datetime, w.user_id, w.status, e.lastname worktimes w inner join employees e on w.user_id = e.id left join (select max(datetime) datetime, user_id worktimes datetime > 1467583200 , location = '16' group user_id ) ww on ww.user_id = w.user_id , w.datetime = ww.datetime order e.nachname;
notes:
- you need join on
datetime
value. - the
right join
unnecessary. replacedleft join
, i'm not sure want either. might startinner join
see if produces want. - do not use
order by
in subqueries in circumstances. - you not need
group by
in outer query
Comments
Post a Comment