-1

I have the following table called TableA

+-----------+--------+--------+
| RequestId | FkLId  | FkSId  |
+-----------+--------+--------+
| 100       | 285301 | 110    |
+-----------+--------+--------+
| 200       | 285301 | 99     |
+-----------+--------+--------+
| 300       | 285301 | 100    |
+-----------+--------+--------+
| 400       | 285301 | 98     |
+-----------+--------+--------+
| 500       | 285301 | 93     |
+-----------+--------+--------+
| 600       | 285302 | 93     |
+-----------+--------+--------+
| 700       | 285302 | 94     |
+-----------+--------+--------+

And also have another table called TableB

+-----------+--------+-------+
| ServiceId | FkLId  | FkSId |
+-----------+--------+-------+
| 500       | 285301 | 109   |
+-----------+--------+-------+
| 501       | 285301 | 99    |
+-----------+--------+-------+

I need to take All the Ids in TableA, not exists in TableB. My Query as follows,

SELECT FkSId
FROM   TableA 
WHERE  FkLId NOT IN (SELECT FkSId FROM TableB) AND FkLId = 285301

But its only returns 110 I need 110,100,98,93 output

Dale K
  • 21,987
  • 13
  • 41
  • 69
Sachith Wickramaarachchi
  • 4,467
  • 5
  • 34
  • 57
  • 2
    Should you change where clause from WHERE FkLID... to WHERE FkSID? – charles Feb 11 '20 at 05:46
  • Change "where FkLId NOT IN" to "where FkSId NOT IN" . – Pankaj_Dwivedi Feb 11 '20 at 05:55
  • Does this answer your question? [SQL - find records from one table which don't exist in another](https://stackoverflow.com/questions/367863/sql-find-records-from-one-table-which-dont-exist-in-another) – philipxy Feb 11 '20 at 08:08
  • This is a faq. Before considering posting please always google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Feb 11 '20 at 08:10

3 Answers3

2

Try this:

SELECT FkSId
FROM   TableA 
WHERE  FkSId NOT IN (SELECT FkSId FROM TableB WHERE FkLId = 285301) AND FkLId = 28530
gotqn
  • 37,902
  • 44
  • 152
  • 231
1

Demo on db<>fiddle

The problem is here

WHERE FkSId NOT IN (SELECT FkSId FROM TableB where FkLId = 285301)

You should change from FkLId to FkSId like below

SELECT FkSId 
FROM TableA 
WHERE FkSId NOT IN (SELECT FkSId FROM TableB where FkLId = 285301) AND FkLId = 285301 

Output

FkSId
110
100
98
93
Nguyễn Văn Phong
  • 12,566
  • 16
  • 32
  • 51
0

You need to change FkLId to FkSId. It will solve your problem.

  SELECT FkSId
    FROM   TableA 
    WHERE  FkSId NOT IN (SELECT FkSId FROM TableB) AND FkLId = 285301
Prajakta Kale
  • 364
  • 3
  • 19