简体   繁体   中英

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

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

I believe your select statement should use an OR:

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM