Solved

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

Posted on 2008-06-13
3
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 143

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

Webinar: Deploying MySQL in production 6/22 11am

Join Percona’s Senior Operations Engineer, Daniel Kowalewski as he presents Deploying MySQL in production on Thursday, June 22, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

728 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