You can use a multi-table delete
statement with a left outer join
and focus on the non-matching rows like this:
delete u from Users as u
left outer join Subscriptionoffering as so
on so.userid = u.userid
where so.userid is null;
Here is some test code to prove it:
mysql> create table Users (userid int unsigned primary key auto_increment) engine = innodb;
Query OK, 0 rows affected (0.43 sec)
mysql> create table Subscriptionoffering (userid int unsigned not null, subscriptionname varchar(32) not null, foreign key (userid) references Users(userid)) engine = innodb;
Query OK, 0 rows affected (0.41 sec)
mysql> insert into Users () values (), (), (), (), ();
Query OK, 5 rows affected (0.38 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from Users;
+--------+
| userid |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+--------+
5 rows in set (0.00 sec)
mysql> insert into Subscriptionoffering (userid, subscriptionname) values (1, 'One'), (3, 'Three'), (5, 'Five');
Query OK, 3 rows affected (0.31 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from Subscriptionoffering;
+--------+------------------+
| userid | subscriptionname |
+--------+------------------+
| 1 | One |
| 3 | Three |
| 5 | Five |
+--------+------------------+
3 rows in set (0.00 sec)
mysql> delete u from Users as u
-> left outer join Subscriptionoffering as so
-> on so.userid = u.userid
-> where so.userid is null;
Query OK, 2 rows affected (0.36 sec)
mysql> select * from Users;
+--------+
| userid |
+--------+
| 1 |
| 3 |
| 5 |
+--------+
3 rows in set (0.00 sec)