Solved

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

Posted on 2008-06-13
3
606 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Why will this INSERT statement not insert the date? 2 19
What is wrong with this PDO Delete Query? 2 17
selector:validator cookies 4 30
Log in through ID 5 17
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

792 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