[英]Mysql Update + SELECT query
I want to update data table for those who score exam id 1,2 more than 80. I try this 我想为那些得分为1,2以上的考试人员更新数据表。我试试这个
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. 它给了我0结果。 But it should have few hundreds results ANy help??
但它应该有几百个结果ANy帮助?
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. 它给出0结果。 How to select those who score more than 80 points for both exam 1 and 2??
如何选择那些在考试1和2中得分超过80分的人?
You query won't work because you're asking for exams that have id = 1 AND id = 2. 您的查询将无效,因为您要求的考试 ID = 1 AND id = 2。
Assuming that id cannot hold two values at the same time, you'll never return any results. 假设id不能同时保存两个值,您将永远不会返回任何结果。
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. 用户只需要谁两种考试得分超过80人。 Assuming personid is a key to the person who took the exam.
假设personid是参加考试的人的关键。
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语句应该使用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
. 因为你不能同时拥有
id ='1'
和id = '2'
,所以在你的情况下返回的值的数量是0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.