4

I have 2 tables for which I need to run a query on

Table1 has 2 fields: l_id, and name

Table2 also has 2 fields: l_id, and b_id

I need to run a query to get the "name" and "l_id" for all the entries in table1 that do not have an entry in table2 for a given b_id.

Hope this makes some sense

2 Answers 2

10
select t1.*
from Table1 t1
left outer join Table2 t2 on t1.l_id = t2.l_id
    and t2.b_id = @SomeValue
where t2.l_id is null
Sign up to request clarification or add additional context in comments.

Comments

3

You can use an outer join, but I find a sub-query is a little more straightforward. In your case selecting everything from table1 that does not have an id in table2. Reads better...

SELECT * FROM table1 WHERE l_id NOT IN (SELECT l_id FROM table2);

2 Comments

this works for me, I just modified that second query to add the b_id condition, but to be fair, RedFilter answered first
No worries. I missed the b_id part anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.