Jump to content

Eliminating results


haku

Recommended Posts

I'm having troubles with a query.

 

Table A:

RID (int)

VALUE (text)

 

Table B:

SID (int)

RID (int)

 

The tables are joined by RID.

 

I want to select all rows from table A with a value of X, but not any that have a row in table B where SID = Y.

 

My query now:

 

SELECT rid

FROM table_a

JOIN table_b

ON table_a.rid = table_b.rid

WHERE table_a.value = "X" AND  table_b.rid != Y

 

The problem arises when there is another row in table_b that has that RID. So for example, lets say I don't want to return any rows with SID = 22, but in table_b I have:

 

Row 1: RID = 1, SID = 22

Row 2: RID = 1, SID = 23

 

I don't want to select table_a.value for RID = 1, because there is a row with SID = 22 and RID = 1. But currently, my query will return the table_a.value for RID = 1, because of Row 2 in my example.

 

Can anyone give me a hand with this? Thanks.

Link to comment
Share on other sites

Got it:

 

SELECT table_1.value

FROM table_1 

LEFT JOIN table_2

ON table_1.rid = table_2.rid

WHERE  table_2.rid NOT IN (SELECT second.rid FROM {table_2} AS second WHERE second.sid = 22)

 

Good work. Almost was about to write an example down.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.