1

The following query does not return any result

SELECT user_id FROM test WHERE  poll_id = 2 AND (question_id = 2 AND answer_id = 5) AND (question_id = 3 AND answer_id = 8);

This is the right syntax or not?

The table data is

|poll_id | question_id | answer_id | user_id |
|--------|-------------|-----------|---------|
| 2      |   2         |  5        |  2      |  
| 2      |   3         |  8        |  2      |  

The result should be 2

2
  • Do you need a user to have both 5 for 2 and 8 for 3, or do you just need either one?
    – Jodaka
    Nov 17, 2011 at 14:07
  • this is what i want, i need user to have both condition satisfed
    – vishal
    Nov 17, 2011 at 14:18

9 Answers 9

6

It doesn't because the AND condition in your where clause is not matched

You should change it to:

SELECT user_id FROM test WHERE  poll_id = 2 
AND 
((question_id = 2 AND answer_id = 5) 
OR (question_id = 3 AND answer_id = 8));

UPDATE

SELECT t.user_id FROM test t WHERE  poll_id = 2 
AND exists (   select 1 from test where 
(question_id = 2 AND answer_id = 5 and user_id=t.user_id ))
AND question_id = 3 AND answer_id = 8 
9
  • i want the result that will satisfy both the result, i.e a user that have (question_id = 2 and answer_id = 5) and (question_id = 3 and answer_id = 8)
    – vishal
    Nov 17, 2011 at 14:21
  • @vishal: I don't think you understand... there's nothing on your data that satisfies BOTH conditions at the same time. I mean, there are 2 rows that match both conditions independently but there's no row that could possibly match BOTH conditions; it's impossible. Look at your data carefully and you'll understand what I mean.
    – Icarus
    Nov 17, 2011 at 14:25
  • @Icarus actually the question was not clear, jodaka's answer is the correct one here.
    – mrembisz
    Nov 17, 2011 at 14:28
  • I think he's saying he wants a user who has answered 5 for question 2 and 8 for question 3: that is, he WANTS to be looking at two independent (except not because of user_id) rows.
    – Jodaka
    Nov 17, 2011 at 14:29
  • @Jodaka and mrembisz, thanks for clarifying this for me. I will update my answer to reflect what I think he wants.
    – Icarus
    Nov 17, 2011 at 14:31
4

You have some difficulties in boolean logic here. Plus, you probably have a typo in a first condition (poll_d) What you probably want is

SELECT user_id FROM test WHERE poll_id = 2 AND 
((question_id = 2 AND answer_id = 5) OR (question_id = 3 AND answer_id = 8));

In your query parentheses do not matter at all, so it can be translated to

SELECT user_id FROM test WHERE  poll_id = 2 
AND question_id = 2 AND answer_id = 5 
AND question_id = 3 AND answer_id = 8;

question_id = 2 AND question_id = 3 according to boolean logic and sane mind is False. The same does for answer_id = 5 AND answer_id = 8

2

Change your AND to an OR in the last condition.

SELECT 
  user_id FROM test 
WHERE  poll_d = 2 
AND (question_id = 2 AND answer_id = 5) 
OR (question_id = 3 AND answer_id = 8);
1
SELECT user_id FROM test WHERE  poll_d = 2 
AND ((question_id = 2 AND answer_id = 5) 
OR (question_id = 3 AND answer_id = 8));
1

Brackets in your where statement has no effect. You have written question_id = 3 AND question_id = 2 , of course it will not return any result! Maybe you need something like this

SELECT user_id FROM test WHERE poll_d = 2 AND ((question_id = 2 AND answer_id = 5) OR (question_id = 3 AND answer_id = 8))
1

You need to use at least one OR and group the ORed part to prevent any precendence bugs:

SELECT user_id FROM test WHERE  poll_d = 2 
  AND ((question_id = 2 AND answer_id = 5) 
       OR (question_id = 3 AND answer_id = 8));

This means: all entries that have poll_d = 2 and either question_id = 2 AND answer_id = 5 or question_id = 3 AND answer_id = 8 (technically using OR would mean "at least one of ..." but in your case there's no way for a row to match both conditions).

Your query could be read as all entries that have poll_d = 2 and both, question_id = 2 AND answer_id = 5 and question_id = 3 AND answer_id = 8 (which is not possible and hence the query has no result).

Note that you'd get 2 results in this case. If you want to get only one, use the distinct keyword: SELECT distinct(user_id) FROM ...

2
  • i want the result that will satisfy both the condition
    – vishal
    Nov 17, 2011 at 14:31
  • @vishal I'm afraid that's not possible in a single select, since no row would satisfy both conditions. You could, however, use count(*) to check whether you got two results or not. If not, at least one condition is not satisfied. Alternatively use a subselect like the the other answers indicate but note that those will get more complicated when you have more conditions (join based solutions might get worse performance since the join would first have to be built).
    – Thomas
    Nov 17, 2011 at 14:46
1

You need to join two records, so:

select p1.user_id from poll p1, poll p2
where p1.poll_id = 2 and p2.poll_id = 2 and 
p1.question_id = 2 and p1.answer_id = 5 and
p2.question_id = 3 and p2.answer_id = 8 and
p1.user_id = p2.user_id
0

Try using the DISTINCT SQL method.

SELECT DISTINCT(user_id) FROM test WHERE  poll_d = 2 
AND ((question_id = 2 AND answer_id = 5) 
OR (question_id = 3 AND answer_id = 8));

And your boolean logic, I used an OR.

PS: Untested.

0

try this.

SELECT user_id FROM test WHERE  poll_d = 2 
AND ((question_id = 2 AND answer_id = 5)  
OR (question_id = 3 AND answer_id = 8));

the reason why its giving 0 records is because filter (WHERE condition) is actually applied to single row. that how the query works to match your filter with every single row.

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.