Jump to content



Photo

SQL Query Help

sql

  • Please log in to reply
2 replies to this topic

#1 ZeroFearX

ZeroFearX

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 08-November 11

Posted 27 December 2013 - 15:48

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




#2 mastermate

mastermate

    Neowinian

  • Joined: 24-February 04
  • Location: Portland,OR

Posted 27 December 2013 - 16:36

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



#3 Fahim S.

Fahim S.

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 15-April 02
  • OS: Windows 8.1 Update 1 / Chrome OS 37
  • Phone: Google Nexus 4

Posted 28 December 2013 - 08:14

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