4

I have a product info table with more than 130 columns/fields.

I want to write a php script that adds a new product to the table OR updates the existing product if it already exist. The first field is the product key.

The product information is stored in a numbered php array : $product_info[0] to $product_info[130].

Basically something like this :

INSERT INTO table (a,b,c) VALUES ($product_info[0],$product_info[1],$product_info[2])
  ON DUPLICATE KEY UPDATE a='$product_info[0]', b='$product_info[1]', c='$product_info[2]'

Is there something more efficient than typing each of the 130 fields twice?

3
  • 6
    Why on earth would you have 130 columns in a table? Or is your terminology wrong in that you want to insert 130 rows / items? Commented Aug 13, 2010 at 15:30
  • As @premiso implies, this probably isn't ideal from a schema perspective. What does this data represent? Commented Aug 13, 2010 at 15:31
  • No its over 130 columns of product details... Not my choice. Commented Aug 13, 2010 at 16:41

3 Answers 3

10

Yes, there is, use the VALUES() function:

INSERT INTO `table` (a, b, c) VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE a = VALUES(a), b = VALUES (b), c = VALUES(c)

Basically, in the UPDATE part, VALUES(column) will return the specified value for that column for the current row in question. So you can do interesting things like:

ON DUPLICATE KEY UPDATE 
    a = VALUES(a), 
    b = VALUES(b) + VALUES(c), 

The beauty of that syntax, is it also supports multiple insert rows:

INSERT INTO `table` (a, b, c) 
    VALUES (?, ?, ?), 
    VALUES (?, ?, ?), 
    VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE a = VALUES(a), b = VALUES (b), c = VALUES(c)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply but I would still have to type all 130 fields twice. This is basically the solution I posted in my question with a little twist. I'm looking for something more efficient if it's doable.
Typo: KYE should be KEY. This is a single-letter change, so I can't do it myself.
1

Unfortunately MySQL does not support merging... having an ORM can help ease the pain of coding multiple IF EXISTS UPDATE ... ELSE INSERT code

Comments

0

REPLACE

4 Comments

No. Don't use REPLACE if you care about referential integrity. It deletes if exists, then inserts. So it'll destroy any foreign key relations.
Right now the php code checks if the row exists. If it does not exist, it does an INSERT. If it does exist, it does a DELETE query followed by an INSERT query.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
@dgw. I never do link-only answers these days. I know better now. Question: Is this answer worth salvaging, or should it just be deleted? I'm busy for the next couple of days, and then on holiday till the end of the month, so I'm not going to fix it any time soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.