If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > On Duplicate Key Update Help Required

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-19-07, 11:14
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
On Duplicate Key Update Help Required

Hi,

I have the following table:

product_names
PD_ID int(10) unsigned NOT NULL,
PD_Name varchar(400) NOT NULL,
PRIMARY KEY (`PD_ID`,`PD_Name`),
KEY PD_Name (`PD_Name`)



I'm trying to run the following query but I keep getting an sql sytax error - any ideas why?

INSERT INTO tbl_product_names (PD_ID, PD_Name)
VALUES (SELECT tbl_products.PD_ID, tbl_supplierproducts.PD_Name FROM
tbl_products, tbl_supplierproducts WHERE tbl_products.PD_Ref = tbl_supplierproducts.PD_Ref) ON DUPLICATE KEY UPDATE;
Reply With Quote
  #2 (permalink)  
Old 08-19-07, 12:19
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
The VALUES clause implies proving contants while the FROM clause implies copying elements from existing tables. You can't have both, so drop the VALUES clause and its parens and you should be closer. Without knowing what you really intended to do, this is only a guess, but it ought to get you started.

-PatP
Reply With Quote
  #3 (permalink)  
Old 08-19-07, 12:31
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by Pat Phelan
The VALUES clause implies proving contants while the FROM clause implies copying elements from existing tables. You can't have both, so drop the VALUES clause and its parens and you should be closer. Without knowing what you really intended to do, this is only a guess, but it ought to get you started.

-PatP
I've tried removing the values clause but i still get the same error.
Instead i've added the following to the end of the on duplicate update clause:

on duplicate key update PD_Name=PD_Name

It seems to work with that. I dont understand why though? I'm not sure if its producing correct results as yet - it needs some more testing.

Basically i want to add values to the product_names table from a supplier_products table. If the name for that product is already in that table I want to simply update using the above on duplicate update clause.
The PD_ID and PD_Name is a compound primary key in the product_nmaes table with PD_ID bieng a foriegn key from the products table.
Reply With Quote
  #4 (permalink)  
Old 08-19-07, 14:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
lets start at the beginning

the PK is ID+Name, right?

so if you attempt to insert another row which has the same ID+Name, what is there to update? nothing

i think you are looking for INSERT IGNORE, not INSERT ON DUPLCIATE KEY
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-19-07, 17:40
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I'm a bit confused (partly why no-one else has mentioned)...
But why is the primary key ID+Name?
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On