0

I have two tables (plannertags and handovertable) where I am trying to JOIN both.

plannertags contains unique entries identified by srNumber whereby handovertable contains a key (plannerTagsId) that link to plannertags's srNumber. But handovertable can have multiple entries that has the same key of plannertags.

Here is my handovertable

enter image description here

Here you can see, there are two plannerTagsId that has the key (plannerTagsId) 17.

What I need

I need to JOIN and query data based on handovertable's latest handoverDate. So what I expect after run query is to get only two entries from handovertable which has the plannerTagsId 16 and 17(that has the latest timestamp). I don't know how to get get latest timestamp and thus I tried latestTag as the selector. Query as below.

$tagQuery ="SELECT p.srNumber, h.handoverFrom, h.handoverRemarks FROM plannertags p
                                JOIN handovertable h
                                on p.srNumber = h.plannerTagsId
                               WHERE (p.status = '0' OR p.status = '2') AND p.currentStage = '1' AND p.assignedTo = '0' AND p.handoverStatus = '1' AND p.failedStatus = '1' AND h.latestTag = '1'
                               ORDER BY p.deliveryDate ASC";

Can someone guide how to tackle this problem using latest time stamp?

Anu
  • 1,123
  • 2
  • 13
  • 42

0 Answers0