Sunday, 15 September 2013

mysql - SQL - return max date with many to many tables -



mysql - SQL - return max date with many to many tables -

i have 3 tables: p, p2l, l

p - p_id; p2l - p_id, l_id l - l_id, date

how max(date) each p_id?

is of query correct?

1

select plot_id, max(l.date) l_maxdate p2l left bring together l on p2l.l_id = l.l_id grouping p_id

2

select plot_id, maxdate.maxldate, p2l inner bring together (select l_id, max(date) maxldate l grouping l_id) maxdate on maxdate.l_id = p2l.l_id grouping p_id

select p.p_id, max(l.date) max_date p inner bring together p2l on p.p_id = p2l.p_id inner bring together l on p2l.l_id = l.l_id grouping p.p_id

but if don't want records table p. can remove table join.

select p2l.p_id, max(l.date) max_date p2l inner bring together l on p2l.l_id = l.l_id grouping p2l.p_id

mysql sql

No comments:

Post a Comment