mysql insert only if the values are different than the max timestamp
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
mysql insert only if the values are different than the max timestamp
I'm trying to insert a row into a mysql table only if the most recent row (the row with the most recent timestamp) differs from the row i'm trying to insert.
can i do that with a single query, or do i have to use a trigger/script?
that wouldn't work. you might have values like this:
from 2010-01-01 to 2010-06-01 - value1
from 2010-06-02 to 2010-07-01 - value2
since 2010-07-02 - value1
so the database would be like:
value ts
value1 2010-01-01
value2 2010-06-02
value1 2010-07-02
having a unique restraint on the values would preclude entering the third entry there, while i want a record of all the values and the times they existed.
Not if you make the constraint over both columns ...
And/or use a separate column for precise timestamps, with the constraint covering only the event column(s) and a less precise timestamp column.
The unit in the less precise timestamp is the unit during which you don't want duplicate reports, for example a day. It does not need to be real timestamp either; you can just divide the number of seconds since epoch ("Unix time") by the number of seconds in your no-duplicate duration to get an integer suitable for the constraint timestamp. Just make sure it is large enough so it won't wrap.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.