?
Solved

Problem with ON DUPLICATE query

Posted on 2012-12-25
9
Medium Priority
?
236 Views
Last Modified: 2012-12-30
Hi,

Something is wrong with this query, but don't know what :(
The query should update an entry if it already exists (country code) or insert it not.

$sql = "
INSERT INTO currency_exchange_rates
	(base, country, value, updated, updated_datetime) 
VALUES
	('{$this->base}', '$code','$item','{$exchangeRates->timestamp}','".date("Y-m-d H:i:s",$exchangeRates->timestamp)."')
ON DUPLICATE
	country
UPDATE
	value='$item', updated_datetime='{$exchangeRates->timestamp}', updated_datetime='".date("Y-m-d H:i:s",$exchangeRates->timestamp)."'
WHERE
	country='$code'
";

Open in new window

0
Comment
Question by:kgp43
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
9 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38720435
Something is wrong with this query
What is the Something in that statement?  Please tell us a little more.  Is the column country an index?  Is there an error message?
0
 
LVL 3

Expert Comment

by:wadehults
ID: 38720444
Here is an article that may be useful to you:
Duplicate Key Update
0
 

Author Comment

by:kgp43
ID: 38720470
Ray_Paseur;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'country UPDATE value='3.672971', updated_datetime='1356480009', updated_d' at line 6

country is an index.

Corrected an error, but still the same error message
$sql = "
INSERT INTO currency_exchange_rates
	(base, country, value, updated, updated_datetime) 
VALUES
	('{$this->base}', '$code','$item','{$exchangeRates->timestamp}','".date("Y-m-d H:i:s",$exchangeRates->timestamp)."')
ON DUPLICATE
	country
UPDATE
	value='$item', updated='{$exchangeRates->timestamp}', updated_datetime='".date("Y-m-d H:i:s",$exchangeRates->timestamp)."'
WHERE
	country='$code'
";

Open in new window


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'country UPDATE value='3.672971', updated='1356480009', updated_datetime='' at line 6

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:kgp43
ID: 38720479
The "Is the column country an index?" question kinda 'tricked' it for me, think I fixed the issue. This seems to work:

$sql = "
INSERT INTO currency_exchange_rates
	(base, country, value, updated, updated_datetime) 
VALUES
	('{$this->base}', '$code','$item','{$exchangeRates->timestamp}','".date("Y-m-d H:i:s",$exchangeRates->timestamp)."')
ON DUPLICATE KEY UPDATE
	value='$item', updated='{$exchangeRates->timestamp}', updated_datetime='".date("Y-m-d H:i:s",$exchangeRates->timestamp)."'
";

Open in new window


I guess "ON DUPLICATE country UPDATE value='$item', updated=....." is not valid.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38720498
Make a Google search for "on duplicate key update mysql php" and see if the answers win the day.  Best regards and merry Christmas, ~Ray
0
 
LVL 3

Expert Comment

by:wadehults
ID: 38720585
The only thing I have to add to this is that the ON DUPLICATE KEY UPDATE command line only works with Unique or Primary Key values.
0
 

Author Comment

by:kgp43
ID: 38720791
Thanks Wadehults, will make sure that is the case :)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38721043
Man Page here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Important Note here:  In general, you should try to avoid using an ON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes.

That tells me that you want country to be the index and the only index.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question