john.p.doe
john.p.doe

Reputation: 521

How to insert records in loop in MySQL based on results from another table

I have a TableA from where I would like some information for e.g. tableA has columns **name, age and rollNo** and I would like to insert **age** and **rollNo** from tableA to tableB wherever the age is greater than 20 and rollNo less than 500

Is there anyway this can be done in MySQL using a procedure or something.

Upvotes: 1

Views: 935

Answers (1)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107518

This can be done in a single query using the INSERT ... SELECT syntax:

INSERT INTO TableB (Age, RollNo)
SELECT Age, RollNo
FROM TableA
WHERE Age > 20 AND RollNo < 500

Upvotes: 2

Related Questions