LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2012, 02:49 AM   #1
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Rep: Reputation: 31
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?
 
Old 05-14-2012, 03:56 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Just create an appropriate unique constraint and let your insert fail.
No farting around needed.
 
Old 05-14-2012, 08:40 PM   #3
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Original Poster
Rep: Reputation: 31
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.
 
Old 05-14-2012, 08:45 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not if you make the constraint over both columns ...
 
Old 05-15-2012, 12:35 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Tinkster View Post
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.
 
Old 05-15-2012, 02:02 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
This is not really a time stamp (which is created by the system) you know. Its just a set of dates.

OK
 
Old 05-15-2012, 09:55 PM   #7
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Original Poster
Rep: Reputation: 31
A constraint over both columns wouldn't work either, because then i might get this:

value1, 2010-01-01 HH:MM:SS
value2, 2010-06-02 HH:MM:SS
value1, 2010-07-02 HH:MM:SS
value1, 2010-07-03 HH:MM:SS

which i also don't want. If the newest entry has the same value as the previous latest entry, then the new entry should be discarded.

this way, you have a record of all the changes and what the value was at any particular time, with as little space used as possible.
 
Old 05-15-2012, 11:26 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
You keep talking about time-stamp, but show us date; now you bring HH:MM:SS
into the equation; which is it?

Either way, I still think that something like
Code:
alter table FOO add unique index(VALUE, date(TIMESTAMP));
should do the trick; discarding the time for the constraint.


Cheers,
Tink
 
Old 05-21-2012, 07:04 PM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Actually,

Code:
Insert into <table> ([field_list]) values (field1, [field2..]]) WHERE m.date_value <> (select max (a.data_value) from <table>)
and so on
http://stackoverflow.com/questions/9...th-a-condition
This is mySql

Oracle has an Insert when clause:
http://psoug.org/reference/insert.html

OK
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mysql query with unix timestamp kirukan Linux - Newbie 3 06-28-2010 06:06 AM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 03:36 PM
Modifying the Linux IP Stack to get TTL min/max/avg values mpk_india Linux - Kernel 0 12-31-2008 04:36 AM
Insert date and timestamp Into File name petenyce Linux - Newbie 9 10-13-2005 12:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration