Finding new entries in a MySQL table
Is it possible to find out if new entries w开发者_如何学Gohere made into a MySQL table?
For example a table has 10 records and someone adds 2 new.
How do I write a php line to detect the new records (only) and insert them into another table?
Any help would be appreciated.
New is relative. So logically you need some anchor/baseline to determine what new means in your system.
A few ideas:
- table has a column 'processes' with a default value of 0, everything which has 0 is new
- table has some time entry, new is when it's younger than time t
- table has a relationship with that other table you mention, every entry in the first table that doesn't have an entry in the second table is new.
- new could also mean the last X autoincrement values.
So you see, in order to get a more precise answer you would need to give more information about what exactly it is that you want to achieve.
You would have to implement some sort of control mechanism for this, for example add a column to your table called (as a crude example) added_into_other_table
. Then check in your query for all records that have a value of zero for added_into_other_table
. Add only these to the new table, then update your original table to set added_into_other_table
to 1 for all of the records you just processed so they won't be processed again.
You can use triggers to achive this.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Syntax will be similar to this one:
CREATE TRIGGER ins_table AFTER INSERT ON first_table
FOR EACH ROW BEGIN
INSERT INTO second_table values(NEW.field1, NEW.field2,..., NEW.fieldn)
END;
精彩评论