I have two tables I want to count the number of ids presented on first which is not in second with respect to user id how can I do the same?
For example.
Case 1
Table 1.
|id|data |User_id
|4 |Some data |12
|5 |Another data |8
|6 |Some other data|6
Table2
|id|table1Id|userId|
|1 |4 |12 |
|2 |5 |14 |
|3 |12 |12 |
Case 2
Table 1.
|id|data |userid
|4 |Some data |12
|5 |Another data |24
|6 |Some other data|32
|7 |Some more data |12
Table2
|id|table1Id|userId|
|1 |4 |12 |
|2 |5 |14 |
|3 |12 |12 |
What I am trying to do is this.
I want to know how many occurrences of id in table1 which is not in table2 for the user 12.
How can I do that?
Case 1 it should return 0
Case2 it should return 1
Best Answer
You can get it by using
NOT EXISTS
First example:
dbfiddle here
Second example:
dbfiddle here