in reply to Re^2: (OT) Dearest Monks - Mysql Question - Compare current insert record with last record inserted.
in thread (OT) Dearest Monks - Mysql Question - Compare current insert record with last record inserted.
(For a simple example, suppose your code glitches and stores several incorrect prices. Easy enough to fix, just get the correct prices and replace. If you're storing calculated price differences, you also need to recalculate. OTOH, if you had a view that did the calculation for you, once you've updated the price the view is automatically fixed.)
You use SQL to compare the prices between rows. Personally, I'd use that SQL to create a view. A good example of such SQL is at MySQL Cookbook.
Update:
If your tables do not have a unique ID column (other than the timestamp), you may need somewhat more awkward SQL. For example, if a table has only two column - timestamp and price - the SQL might be:
select new.timestamp, new.price, new.price - old.price as price_change from tablename new left join tablename old on old.timestamp = ( select max(sub.timestamp) from tablename sub where sub.timestamp < new.timestamp )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: (OT) Dearest Monks - Mysql Question - Compare current insert record with last record inserted.
by jdlev (Scribe) on Nov 19, 2009 at 21:13 UTC | |
by keszler (Priest) on Nov 20, 2009 at 02:33 UTC |