# Creating multiple records in MySQL

**URL:** <https://forum.inductiveautomation.com/t/creating-multiple-records-in-mysql/1950>\
**Category:** Archived Topics\
**Created:** [June 17, 2010, 4:35pm UTC](https://forum.inductiveautomation.com/t/creating-multiple-records-in-mysql/1950 "2010-06-17T16:35:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [June 17, 2010, 4:35pm UTC](https://forum.inductiveautomation.com/t/creating-multiple-records-in-mysql/1950/1 "2010-06-17T16:35:45Z")

</div>

I have 2 tables, A and B. A contains 100 records, B contains a subset of 80 of those records. Both tables use ID as an index.

I want to create 20 new records in B for the records that are missing when compared with A.

Is it possible to do this in SQL rather than use a programming language? I know MySQL has a number of looping constructs, but I don’t know whether they can be used in this kind of scenario.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [June 17, 2010, 5:59pm UTC](https://forum.inductiveautomation.com/t/creating-multiple-records-in-mysql/1950/2 "2010-06-17T17:59:35Z")

</div>

I managed to work this one out:`INSERT INTO B (ID,Date)
SELECT A.ID,'1970-01-01 00:00'
FROM A LEFT JOIN B
ON A.ID=B.ID
WHERE B.ID IS NULL`By using a LEFT JOIN, all records in A are returned with all matching records in B. When a matching B record is not found, the B fields are set to NULL. By including a search for NULL in the WHERE clause, the SELECT statements returns all the IDs in A which don’t have a match in B.

In this case I’m also setting another field in B to a constant value for each new record. The fields to be updated in B are specified inside the brackets in the first line.
