-2

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
  • 25,246
  • 15
  • 42
  • 71
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • 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
  • 42,737
  • 46
  • 157
  • 243
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
  • 13,506
  • 17
  • 39
  • 56
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
  • 392
  • 3
  • 19