开发者

select records that do not exist in a select

I need to track changes created in a directory and also saved the history. I have a function that scans the files in that directory then it inserts them in a table. let's say that the first time this program was run there where files A, and B. As a result the table should look like

FileID     File      DateModified       
  1         A          101010     
  2         B          020202

let's say the user modifies file B therefore the next time the program runs the table should look like:

FileID     File      DateModified       
  1         A          101010     
  2         B          020202              
  3         A          101010     
  4         B          030303

From looking at the table above we know that file B has been changed because it has a different modified date and also that file A was not modified. Moreover my program know that the records that where inserted are all the records with a fileID greater than 2. How could I perform a select that will return the last file B because that file was modified. I want to be able to know which files have been modified how could I build that query.

Please read above first in order to understand this part. Here is another example.

First time program runs:

FileID     File      DateModified       
  1         X          101010     
  2         Y          020202

Next time program runs:

FileID     File      DateModified       
  1         X          101010     
  2         Y          020202   
  3         Y          020202            
  4         A          010101

so far we know that file X has been deleted because it is not included in the new scan. Moreover we know that file A has been created. And lastly that File Y was not modified it is the same. I would like to perform a select where I can just get the files that where created or modified such as file A in this case.

I am looking for something like:

select * from table1 where fileID > 2 AND File NOT IN (SELECT File FROM table1 WHERE File <=2) AND DateModified NOT IN (SELECT DateModified FROM table1 WHERE File <=2)

I don't know why is it that when I perform such query I get dif开发者_StackOverflowferent results. Maybe I will have to group the File and DateModified into one column to make it work.


I would add a column called scan_number so that you can compare the latest scan with the previous scan.

SELECT  curr.file, prev.file, curr.DateModified, prev.DateModified
FROM    table1 curr
LEFT
JOIN    table1 prev
on curr.file = prev.file
and curr.scan_number = 100
and prev.scan_number = 99
WHERE   curr.DateModified != prev.DateModified
OR      curr.file IS NULL
OR      prev.file IS NULL

If you want to catch inserts and deletes, you need full outer join, but it seems sqlite doesn't support that. You might have to run the query twice, once to find inserts and updates, and once to find deletes.


DateModified is being asked to perform too many jobs: it is used to track both the file modification date and proof-of-existence for a given filename on a given date.

You could add another column, ScanId, a foreign key to a new table ScanDates that records a scan id and the date the scan was run.

That way, you could inspect all the results with a given ScanId and compare against any selected previous scan, and also keep track of the real modification dates of your files.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜