Skip to main content

Posts

Showing posts from March, 2018

Inserting and updating multiple rows by using INSERT and SELECT subqueries

How to insert data from one table to another table efficiently? How to insert data from one table using where condition to another table? Let's have a look on it:- SELECT subquery in the INSERT statement can be used to add values into a table from one or more other tables or views. Using a SELECT subquery also lets more than one row be inserted at the same time. INSERT INTO `table1`  (`id`, `user_id`, `name`, `is_default`,  `entity_status`) SELECT NULL, table2.id, 'General', 1, 'Active' FROM table2; Above example will retrieve the all the id from table2 and insert it into table1 with the other data. UPDATE ` table1 `  JOIN table2 on  table2 .academic_session_id =  table1 .academic_session_id SET  table1 .fee_structure_master_id=  table2 .id WHERE  table2 .is_default = 1; Above example will retrieve the all the id from table2 and update it into table1