Sign in to follow this  
Followers 0

sql SQL Query Help

3 posts in this topic

Posted

Hello,

 

I have a SQL question.

 

Let say that I have two tables.

 

id

--

1

2

3

 

 

sid id type
---  -- ------
1    1  1

2    2  1

3    3  1

4    1  2

5    2  2

6    1  3

 

I am trying to get returned t1.id, where t2.type = 1 and t2.type is not in ('2', '3')

From the example showed previously, only t1.id that would be returned is '3'.

 

 

Sorry for the bad explanation but I don't know how to explain it better.

 

Thanks for the heklpo

Share this post


Link to post
Share on other sites

Posted

select t1.id from t1 ,t2
where t1.id=t2.id
and t2.type=1
and t2.type not in (2,3)

The "not in" clause is redundant  as it is essentially the same as the "=1" clause.

This returns id's of 1,2 and 3.

So given the criteria returning only an id =1 is not possible

Share this post


Link to post
Share on other sites

Posted

Does table 2 have a foreign key relating it back to table 1?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.