Task: to execute a query that depends on the execution of an in operator request That is, the request will be made from the admin panel of a certain super-user who has a list of other referral users who were previously registered with him. It is necessary that when deleting it is checked whether there is a referral in the super-user list.

Sample code:

DELETE FROM (`users`) WHERE `user_id` IN (SELECT `user_id` FROM `users` WHERE `referrals` = `super_user_id`) AND `status` = '1' 

Table users

  |  user_id |  referrer_id |  name |  status |
 -----------------------------------------
 |  1000 |  0 |  user1 |  1 |
 -----------------------------------------
 |  1001 |  0 |  user2 |  1 |
 -----------------------------------------
 |  1002 |  0 |  user3 |  0 |
 -----------------------------------------
 |  1010 |  1000 |  user4 |  1 |
 -----------------------------------------
 |  1020 |  1000 |  user5 |  1 |
 ----------------------------------------- 

In this case, the super-user (referrer_id = 0) with user_id = 1000 has two referrals user_id (1010 , 1020) . When deleting any of them, you need to check whether they are the referrals of the user with user_id = 1000. Because when deleting, you can specify the value of user_id = 1001 and delete another super-user.

  • one
    You would write in the request which column from which table, but nothing is clear. - Mike
  • In general, you need to delete A.* from users A JOIN users B on ....
  • What is meant to check whether there is a referral ? In this case, do not delete? - nick_n_a
  • Added a description. - haravares

0