945,066 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 4252
  • MySQL RSS
Jul 30th, 2010
0

Mysql - How to use UPDATE with SELECT???

Expand Post »
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
http://www.ephpsolutions.com/scud/rr...t_category.gif


related_category table
http://www.ephpsolutions.com/scud/rr...d_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:
MySQL Syntax (Toggle Plain Text)
  1. UPDATE related_category
  2. SET related_category.rel_cat_name =
  3. (
  4. SELECT product_category.cat_name FROM product_category
  5. INNER JOIN related_category
  6. ON related_category.rel_cat_id = product_category.cat_id
  7. )

But it doesn't works,
Please assist. Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rahulephp is offline Offline
12 posts
since Jun 2010
Jul 30th, 2010
0
Re: Mysql - How to use UPDATE with SELECT???
Try this one:

MySQL Syntax (Toggle Plain Text)
  1. UPDATE related_category
  2. SET related_category.rel_cat_name =
  3. (
  4. SELECT product_category.cat_name FROM product_category
  5. WHERE related_category.rel_cat_id = product_category.cat_id LIMIT 1
  6. )
Reputation Points: 18
Solved Threads: 11
Junior Poster in Training
aquilax is offline Offline
64 posts
since Jul 2010
Jul 30th, 2010
1
Re: Mysql - How to use UPDATE with SELECT???
Thank you buddy,
I got the solutions. Here it is:

MySQL Syntax (Toggle Plain Text)
  1. UPDATE related_category
  2. INNER JOIN
  3. product_category
  4. ON related_category.rel_cat_id = product_category.cat_id
  5. SET related_category.rel_cat_name = product_category.cat_name
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rahulephp is offline Offline
12 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Mysql error
Next Thread in MySQL Forum Timeline: Booking System - How to deal with double bookings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC