Jump to content

Mysql - How to use UPDATE with SELECT???


rahulephp

Recommended Posts

Mysql - How to use UPDATE with SELECT???

 

I have two tables:

1) products_category

-cat_id

-cat_name

 

2) related_category

-rel_cat_id (exaclty same as products_category.cat_id==related_category.rel_cat_id)

-rel_cat_name

 

products_category table

product_category.gif

 

 

related_category table

related_category.gif

 

 

I want to get cat_name from products_category and want to store in rel_cat_name in related_category tabel. and the query should be only one.

 

Not sure how will it works.

I thought it would be something like:

UPDATE related_category 
SET related_category.rel_cat_name = 
( 
SELECT product_category.cat_name FROM product_category
INNER JOIN related_category
ON related_category.rel_cat_id = product_category.cat_id
)

 

But it doesn't works,

Please assist. Thanks in advance

Link to comment
Share on other sites

Not at all.

 

Use join in UPDATE's table reference clause

 

UPDATE related_category AS rc 
INNER JOIN product_category AS pc 
ON rc.rel_cat_id = pc.cat_id
SET rc.rel_cat_name = pc.cat_name

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.