Jump to content

[SOLVED] INSERT......ON DUPLICATE question


asmith3006

Recommended Posts

Hi,

 

I'm having trouble getting my head around INSERT...... ON DUPLICATE at the moment.

 

I have three tables,

 

Product details:

id, description.....

 

Categories

type, value

 

Product Categories

product_id, category_value

 

Example data for the Categories table:

height, short

height, tall

colour, red

colour, blue.

 

What I want to do is have no more than one entry for each category 'type' for each product (i.e. a product can be tall and red but not tall and short).

 

I was hoping to do this with INSERT...ON DUPLICATE as this means when a user changes a product from red to blue I don't have to do "if colour exists delete colour insert new colour".

 

Thanks for any and all help!

Link to comment
https://forums.phpfreaks.com/topic/148908-solved-inserton-duplicate-question/
Share on other sites

You should create a trigger BEFORE INSERT to Product Categories table. Inside you should check, if you should insert or update.

 

Alternatively, you could do a small change to your table design

 

Product Categories
product_id, category, category_value
1, 'height', 'tall'
1, 'color', 'red'
2, 'color', 'blue'

 

and create a unique key on (product_id, category)

then you could use INSERT ... ON DUPLICATE KEY UPDATE

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.