views:

439

answers:

3

I want to update data table for those who score exam id 1,2 more than 80. I try this

UPDATE data
SET 
column = 'value'
WHERE
(SELECT * FROM exams
WHERE (id = '1' AND score >= 80) AND (id = '2' AND score >= 80));

It gives me 0 result. But it should have few hundreds results ANy help??

I think the problem is this:

SELECT * FROM exams
WHERE (id = '1' AND score >= 80) AND (id = '2' AND score >= 80)

It gives 0 result. How to select those who score more than 80 points for both exam 1 and 2??

+6  A: 

You query won't work because you're asking for exams that have id = 1 AND id = 2.

Assuming that id cannot hold two values at the same time, you'll never return any results.

Try this as the basis of your update instead :-

SELECT * FROM exams
WHERE score >= 80 AND id IN ( '1','2' )

Edited based on comment :-

User wants only people who scored more than 80 for both exams. Assuming personid is a key to the person who took the exam.

SELECT e1.personid FROM
(
   SELECT personid FROM exams  WHERE score >= 80 AND id = '1' 
) e1
INNER JOIN
(
   SELECT personid FROM exams  WHERE score >= 80 AND id = '2' 
) e2
ON
  e1.personid = e2.personid
Paul Alan Taylor
this is the right way to go about it.
Anirudh Goel
erm.. i tested. But an user, he only score exam 1 that above 80, but this query still giving the result. My requirement is, the user must score above 80 for both exam 1 and 2. Either one below 80 should not include
mysqllearner
See if the above code works for you.
Paul Alan Taylor
WOW.. After some adjustment, it works. Thanks. I based on this method though. Really, i cant thank you enough!!!!!!
mysqllearner
+2  A: 

I believe your select statement should use an OR:

SELECT * FROM exams
WHERE (id = '1' AND score >= 80) OR (id = '2' AND score >= 80)
hoffmandirt
I think this does not work. The user must meet 2 requirements. Exam 1 and 2 must be above 80. Ur query return exam 1 or exam 2 even the user only score exam 1 above 80
mysqllearner
Gotcha. Paul Alan Taylor has your answer then.
hoffmandirt
+1  A: 

Try this:

SELECT * FROM exams
WHERE (id = '1' OR id = '2') AND score >=80

Because you cannot have id ='1' and id = '2' simultaneously the number of values returned in your case is 0.

Prasoon Saurav