开发者

Basic join query understanding

I know this very silly, but can anybody hel开发者_StackOverflow社区p 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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜