Solved

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

Posted on 2008-06-13
3
607 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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