views:

48

answers:

1

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...

+2  A: 

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.

Peter Lang
That means if I have multiple values of trackid 1 and 2 both then they will displayed as per maximum changedone value first and then less than that and so on ???
OM The Eternity
@OM: Not sure if I understand your comment. As long as `changedone` is unique per `trackid` you will get only one row per `trackid`, the one with the highest value of `changedone`.
Peter Lang
ok grt Thats what I wanted to know
OM The Eternity