ChatGPT解决这个技术问题 Extra ChatGPT

How to exclude rows that don't join with another table?

I have two tables, one has primary key other has it as a foreign key.

I want to pull data from the primary table, only if the secondary table does not have an entry containing it's key. Sort of an opposite of a simple inner join, which returns only rows that join together by that key.


P
Pranay Rana

https://i.stack.imgur.com/oEEDZ.png

SELECT <select_list> 
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

https://i.stack.imgur.com/pxUO3.png

From aticle : http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx


finally! why don't they have these in text books and why did my lecturers at uni not have these?! they used the worst possible explanations in the world, not remotely close to these!
This is gold. I hate to write a comment that has no content but effusive praise, but come on! This is an awesome answer. Thank you, @Pranay Rana.
Please explain for me, why B.Key IS NULL but we still comparable A.Key = B.Key?
@DoNhuVy simple, the comparison is in the "ON" clause, in a LEFT or RIGHT join if there is no matching row then a row with all NULLs is joined, after which you test for IS NULL to know that there is no matching row. (This only works, by the way, if the field you are testing is NOT NULL, i.e. cannot have NULL for some other reason)
I am using the below query: SELECT A.* FROM #PurgeFilesListNew A FULL OUTER JOIN #DoNotPurgeFilesListNew B ON A.JobFileId = B.JobFileId AND A.AccountID = B.AccountID WHERE A.JobFileId IS NULL OR B.JobFileId IS NULL AND A.AccountID IS NULL OR B.AccountID IS NULL Basically, I am having two comparing two values in "ON" clause. It is working fine, but the query is returning null row for those rows which are not matched. How to solve this? Please help
g
gbn
SELECT
   *
FROM
   primarytable P
WHERE
   NOT EXISTS (SELECT * FROM secondarytable S
     WHERE
         P.PKCol = S.FKCol)

Generally, (NOT) EXISTS is a better choice then (NOT) IN or (LEFT) JOIN


well he didn't post what DBRMS is used, however in MySql LEFT JOIN outperforms NOT EXIST
@The Scrum Meister: did I say faster? Search for IN vs EXISTS vs JOIN to discover the semantic and logical differences...
@gbn Sorry, i thought by "better choice" you meant faster. Can you then please explain in what way is it a better choice? explainextended.com/2009/09/18/…
@The Scrum Meister: generally, any kind of JOIN may need a DISTINCT. NOT IN with a null in the list gives false. IN/EXISTS behave the same. However, the only "safe" construct is (NOT) EXISTS unless you like inconsistency
D
David Sherret

use a "not exists" left join:

SELECT p.*
FROM primary_table p LEFT JOIN second s ON p.ID = s.ID
WHERE s.ID IS NULL

U
Unheilig

Another solution is:

SELECT * FROM TABLE1 WHERE id NOT IN (SELECT id FROM TABLE2)

T
Tommi
SELECT P.*
FROM primary_table P
LEFT JOIN secondary_table S on P.id = S.p_id
WHERE S.p_id IS NULL

Got a question.. if we are using the condition P.key = S.key and then say where S.key IS NULL , then doesn't that make P.key null as well ?
P
Pranay Rana

If you want to select the columns from First Table "which are also present in Second table, then in this case you can also use EXCEPT. In this case, column names can be different as well but data type should be same.

Example:

select ID, FName
from FirstTable
EXCEPT
select ID, SName
from SecondTable

I tried this for Progress OpenEdge DB and it doesn't work. Would you be able to mention the RDBMS(s) you know this works for?
J
JennyB

This was helpful to use in COGNOS because creating a SQL "Not in" statement in Cognos was allowed, but it took too long to run. I had manually coded table A to join to table B in in Cognos as A.key "not in" B.key, but the query was taking too long/not returning results after 5 minutes.

For anyone else that is looking for a "NOT IN" solution in Cognos, here is what I did. Create a Query that joins table A and B with a LEFT JOIN in Cognos by selecting link type: table A.Key has "0 to N" values in table B, then added a Filter (these correspond to Where Clauses) for: table B.Key is NULL.

Ran fast and like a charm.