473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

INSERT INTO and ON DUPLICATE KEY

jtw
I need to update a statistics table through out the day. I would like
to insert the first data sampling of the data, then update the
existing record for the rest of the day. The problem is that I do not
alway know when the first sampling will start.

Keys for the table are username and date. So each user will have only
one entry per day.

I have been checking to see if the record exists - select .. where
username=... AND date =.... Seems like there is a better way.
I found the 'ON DUPLICATE KEY' in the mysql manual. Just can not get
this to work with PHP.
Sample would be.

INSERT INTO usagestats (username, date, usageinfo)
VALUES(username = 'jon', date = '2003-09-19', useageinfo='250315')
ON DUPLICATE KEY UPDATE usageinfo = '250315'
In this case if the record with username='jon' and date='2003-09-19'
already exists, then the ON DUPLICATE KEY would update this record
with usageinfo = '...'
What am I missing?

jtw

--

http://www.UsenetRocket.com
Jul 17 '05 #1
2 16399
On Fri, 19 Sep 2003 16:46:58 -0400, jtw <jw*****@mailboxspot.com> wrote:
I need to update a statistics table through out the day. I would like
to insert the first data sampling of the data, then update the
existing record for the rest of the day. The problem is that I do not
alway know when the first sampling will start.

Keys for the table are username and date. So each user will have only
one entry per day.

I have been checking to see if the record exists - select .. where
username=... AND date =.... Seems like there is a better way.
Depending on whether inserts or updates are more likely:

INSERT the row - catch the key violation (1036) error - if you got one, do an
UPDATE instead.

or

UPDATE the row - check mysql_affected_rows - if zero, do an INSERT.
I found the 'ON DUPLICATE KEY' in the mysql manual. Just can not get
this to work with PHP.

Sample would be.

INSERT INTO usagestats (username, date, usageinfo)
VALUES(username = 'jon', date = '2003-09-19', useageinfo='250315')
ON DUPLICATE KEY UPDATE usageinfo = '250315'

In this case if the record with username='jon' and date='2003-09-19'
already exists, then the ON DUPLICATE KEY would update this record
with usageinfo = '...'

What am I missing?


The error message or unexpected behaviour you're seeing, and your MySQL
version.

The manual says that syntax isn't available until MySQL 4.1.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
jtw
Thanks Andy. Sometimes the simple solutions are the best.

jtw

On Fri, 19 Sep 2003 22:04:30 +0100, Andy Hassall <an**@andyh.co.uk>
wrote:
On Fri, 19 Sep 2003 16:46:58 -0400, jtw <jw*****@mailboxspot.com> wrote:
I need to update a statistics table through out the day. I would like
to insert the first data sampling of the data, then update the
existing record for the rest of the day. The problem is that I do not
alway know when the first sampling will start.

Keys for the table are username and date. So each user will have only
one entry per day.

I have been checking to see if the record exists - select .. where
username=... AND date =.... Seems like there is a better way.


Depending on whether inserts or updates are more likely:

INSERT the row - catch the key violation (1036) error - if you got one, do an
UPDATE instead.

or

UPDATE the row - check mysql_affected_rows - if zero, do an INSERT.
I found the 'ON DUPLICATE KEY' in the mysql manual. Just can not get
this to work with PHP.

Sample would be.

INSERT INTO usagestats (username, date, usageinfo)
VALUES(username = 'jon', date = '2003-09-19', useageinfo='250315')
ON DUPLICATE KEY UPDATE usageinfo = '250315'

In this case if the record with username='jon' and date='2003-09-19'
already exists, then the ON DUPLICATE KEY would update this record
with usageinfo = '...'

What am I missing?


The error message or unexpected behaviour you're seeing, and your MySQL
version.

The manual says that syntax isn't available until MySQL 4.1.


--

http://www.UsenetRocket.com
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
18
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10)...
11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
10
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within...
3
by: Hai Nguyen | last post by:
Hi all I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field (PR) (PR) ID Project Ans 1 2 a 1 ...
2
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers...
6
by: reppisch | last post by:
Hi Ng, I have a multiset for keeping elements sorted in a container but key values may also be equal. Is there any guaranteed traversal order within the duplicate keys of a multimap? When...
2
by: Weyus | last post by:
All, Just want to make sure that I understand what's going on here. I have a table with IGNORE_DUP_KEY set on a unique, multi-column index. What I'm seeing is this: 1) When performing a...
6
by: Dave | last post by:
I really don't like the users getting an unhandled expception page, and I'm still to new with ASP.Net and C#. So please accept my appology for the 2 part question. SqlException (0x80131904) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.