mysqlsqlsql-serverdatabasessms-16

Insert the data into Table C IF The data is not in Table B while Comparing to Table A?


I have 2 tables i want to compare the data and insert the difference into 3rd table for example i have

table  A

number  Status
1111    C
2222    O
3333    O
4444    C


Table B 

number status
1111    c
2222    O
5555    C
6666    O

Now i want to insert data from Table A which is missing from Table B i.e. 3333 and 4444

I have this query

Insert into Table C (number,Status)
Select a.number,a.Status from Table A a
left join Table B  b ON b.number = a.number
where B.number is Null

But it is inserting Null data into Table C


Solution

  • It works perfectly fine.

    enter image description here

    Please check the db<>fiddle here.