Tuesday, October 15, 2019

MySQL Insert or Update column with value from another table

INSERT

Sometimes, there was an old data that need to be transfer from old table to new table. However, not all fields are required to transfer. So, below is the example of two tables and fields, and the sql statement.
tableOld
id     name     ic_no     phone_no     address
tableNew
id     name     ic_no     phone_no     

MySQL statement

INSERT INTO tableNew (name,ic_no,phone_no)
  SELECT name,ic_no,phone_no FROM tableOld 
  WHERE tableOld.id > 1;
This SQL statement is called as insert into select.


UPDATE

In this case, I want to get value from table1 to update into table2 based on certain condition or value.
For example :-
table1
id     name     ic_no     phone_no
table2
id     name     phone_no     address
I want to get phone_no from table1 and update the value phone_no into table2 with condition table2.name = table1.name.

MySQL statement
UPDATE table2 t2 
        INNER JOIN table1 t1 
             ON t2.name = t1.name
SET t2.ic_no= t1.ic_no
WHERE t2.name = t1.name;

IONIC PASS PARAMETER

 To pass parameter to next page, first we need to make sure that we have 2 pages which is page from and page to. In page from ts file, you h...