Solved

MYSQL PHP: How to update a column with value in another table?

Posted on 2008-06-13
3
599 Views
Last Modified: 2013-12-13
PHP MYSQL


Hello,

How do I insert value into a column based on value from another table?

For example, I want to insert the Category Name into all rows in TABLE 1, by checking their Category ID and obtaining the Category Name from TABLE 2.

TABLE 1
[ code ] [ category id ] [ category name ]
[ 001 ] [ 50 ] [  ]
[ 002 ] [ 60 ] [  ]
[ 003 ] [ 70 ] [  ]

TABLE 2
[ category id ] [ category name ]
[ 50 ] [ Toys ]
[ 60 ] [ Books ]
[ 70 ] [ CD ]





How do I write a mysql query or PHP script to do this?

Thanks.

0
Comment
Question by:gingera
3 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 21777070
You can do it in SQL
UPDATE table1, table2 set table1.categoryname =table2.cateoryname WHERE table1.categoryid=table2.categoryid

http://dev.mysql.com/doc/refman/5.0/en/update.html
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 21777080
here we go:
UPDATE table1, table2
  SET table1.category_name = table2.category_name
 WHERE table1.caetgory_id = table2.category_id

Open in new window

0
 

Author Closing Comment

by:gingera
ID: 31466863
THANKS!!!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now