1

Need to check for the status 0 or if there is no record in the table.

But only returns me error.

Where I am going wrong?

My Code:

SELECT session_id
FROM t_table_1
WHERE session_id NOT IN (select distinct session_id from t_table_2) 
OR (select distinct session_id from t_table_2 where status = '0')

I want the result show me the session that does not exist and status equal 0

2
  • add session_id not in again before the next subquery
    – Excel
    Jul 16, 2015 at 11:17
  • combine the two subqueries using union.. the or is looking for a boolean expression not a continuation of the in clause..
    – amdixon
    Jul 16, 2015 at 11:17

3 Answers 3

0

SELECT session_id FROM t_table_1 WHERE session_id NOT IN (select distinct session_id from t_table_2) OR session_id NOT IN (select distinct session_id from t_table_2 where status = '0')

OR

SELECT session_id FROM t_table_1 WHERE session_id NOT IN ((select distinct session_id from t_table_2) union (select distinct session_id from t_table_2 where status = '0'))

Both the above queries should work

3
  • Exist session_id check in status = '0', OR session_id EXISTS (select distinct session_id from t_table_2 where status = '0'), not working.
    – lemon 13
    Jul 16, 2015 at 11:25
  • use not in only instead of exists and try. It is working with sample data
    – Excel
    Jul 16, 2015 at 11:29
  • I want the result show me the session that does not exist and status equal 0
    – lemon 13
    Jul 16, 2015 at 11:33
0

Why you use two subquery when you can get same result ?

using single sub query for E.g

SELECT session_id
FROM t_table_1
WHERE session_id NOT IN (select distinct session_id from t_table_2) 

because you are using NOT IN when your first Sub query return all rows of table

select distinct session_id from t_table_2
2
  • I want the result show me the session that does not exist and status equal 0
    – lemon 13
    Jul 16, 2015 at 11:33
  • @lemon13 yes I am trying to explain same thing that you not need to use two condition .. in comment you said that you want record that session not exists .. then .. Jul 16, 2015 at 11:36
0

Three things:

  • Use not exists rather than not in. If there is a NULL value in the subquery, then not in does not behave intuitively.
  • No need for select distinct in the subquery.
  • Don't put a bare subquery in the where clause. Use exists.

Hence:

SELECT t.session_id
FROM t_table_1 t
WHERE NOT EXISTS (select 1 from t_table_2 t2 where t2.session_id = t.session_id) OR
      NOT EXISTS (select 1 from t_table_2 where status = '0');

I am not sure if this is exactly the logic your specify in the text (which is a bit vague). Sample data and desired results are a big help in clarifying questions.

1
  • I want the result show me the session that does not exist and status equal 0
    – lemon 13
    Jul 16, 2015 at 11:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.