Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Update + SELECT query

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??

like image 590
mysqllearner Avatar asked Dec 11 '25 04:12

mysqllearner


2 Answers

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
like image 103
Paul Alan Taylor Avatar answered Dec 12 '25 21:12

Paul Alan Taylor


I believe your select statement should use an OR:

SELECT * FROM exams
WHERE (id = '1' AND score >= 80) OR (id = '2' AND score >= 80)
like image 38
Dan Polites Avatar answered Dec 12 '25 20:12

Dan Polites



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!