Shahid Ahmed Shahid Ahmed - 3 months ago 5
MySQL Question

Extracting record from table skip record of other table

Having two tables
1. Parent Users
2. Child Users

enter image description here

Child User table also has records of Parent Users.
I want an output of All Childs only.

Answer

The better way to take one more column in child User with flag ( 1 for parant user, 0 for child user ) like:

          child users
-------------------------------------
| id  |  name  |  by_id   |  flag   |
-------------------------------------
| 1   |  x     |  1       |   1     |
| 2   |  a     |  1       |   0     |
| 3   |  b     |  1       |   0     |
| 4   |  z     |  3       |   1     |
| 5   |  c     |  3       |   0     |
| 6   |  y     |  2       |   1     |
| 7   |  d     |  2       |   0     |
| 8   |  e     |  1       |   0     |


To get all child entries:

SELECT id, name FROM child_users WHERE flag = 0;
Comments