OM The Eternity
OM The Eternity

Reputation: 16244

Basic join query understanding

I know this very silly, but can anybody help me in understanding what does this join query is doing in elabortive description?

SELECT j1.*
FROM jos_audittrail j1
LEFT OUTER JOIN jos_audittrail j2
   ON (j1.trackid = j2.trackid AND j1.field = j2.field AND j1.changedone < j2.changedone)
WHERE j1.operation = 'UPDATE'
  AND j1.trackid=$t_ids[$n]
  AND j2.id IS NULL

I know its very silly, but i need to go ahead with my further need... Pls do help me...

Upvotes: 1

Views: 190

Answers (1)

Peter Lang
Peter Lang

Reputation: 55624

The Left Join in combination with j2.id IS NULL returns only those rows of j1, where no row of j2 can be found.

Since the condition is j1.changedone < j2.changedone, it returns only the rows with the highest changedone per trackid (if there is more than one row with this value of changedone for a trackid, all of them are returned).


So if you have

trackid  changedone
      1           1
      1           2
      2           1

You will get

trackid  changedone
      1           2
      2           1

since for 1 - 1 the Left Join finds a record (1 - 2), so j2.id is NOT NULL.

Upvotes: 2

Related Questions