SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Updating another table with data from main table

It is often required to use one table and update data in another table. The second table columns gets updated by taking data from first table. Let us say we have one table where students test marks are stored along with other details in other columns. We can collect only the test data and keep them in a separate table. Let us call this second table as student mark table. Both the tables will have student id field which we will be using to link both tables







update s_mark,s_test set mark= test1 where s_mark.s_id=s_test.s_id

Here are both tables for you.

CREATE TABLE `s_mark` (
  `s_id` int(3) NOT NULL,
  `mark` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `s_mark`
--
INSERT INTO `s_mark` VALUES (1, 7);
INSERT INTO `s_mark` VALUES (2, 6);
-- --------------------------------------------------------
--
-- Table structure for table `s_test`
--
CREATE TABLE `s_test` (
  `s_id` int(3) NOT NULL,
  `test1` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Discuss this tutorial at forum

List of SQL Tutorials


Further readings
Creating a new table by using data from one table
Copying data from one table to another table
Updating another table with data from main table
Update SQL commands
Inserting SUM, AVG data from one table column to other using group by command
Create table query with if exists sql with php script
 
Scripts
PHP
JavaScript
HOME
SQL Tutorial List
SQL (Home)
SQL Commands
AVG
Alter Table
Between
Count
Copy Table
Create Table
Delete
Distinct
Group by
Having
Insert
Inner Join
IN
Left join
Limit
Like
MAX
MIN
Order By
OR AND
Rand
Replace
Rename Table
Select Query
Sum
Union
Update
Where
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.