示例:
表Question_Answers
:
+------+--------+
| q_id | ans_id |
+------+--------+
| 1 | 2 |
| 1 | 4 |
| 2 | 1 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
+------+--------+
User_Submited_Answers
:
| q_id | sub_ans_id |
+------+------------+
| 1 | 2 |
| 1 | 4 |
| 2 | 1 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
+------+------------+
如果此行匹配count 1 else 0
,我需要一个T-SQL查询答案 0 :(得分:0)
let restaurantLat = restaurants["results"][indexPath.row]["geometry"]!["location"]!!["lat"] as? String
答案 1 :(得分:0)
请尝试以下代码:
select qa.q_id,case when qa.ans_id=sqa.ans_id then 1 else 0 end as result from questionans qa
left join subquestionans sqa
on qa.q_id=sqa.q_id and qa.ans_id=sqa.ans_id
答案 2 :(得分:0)
这应该可以为每个问题提供预期的结果。
select q_id, min(Is_Correct)Is_Correct from (
select Q.q_id,case when count(A.sub_ans_id)=count(*) then 1 else 0 end as Is_Correct
from #Q Q left join #A A on Q.q_id=A.q_id and Q.ans_id=A.sub_ans_id
group by Q.q_id
UNION ALL
select A.q_id,case when count(Q.ans_id)=count(*) then 1 else 0 end as Is_Correct
from #Q Q right join #A A on Q.q_id=A.q_id and Q.ans_id=A.sub_ans_id
group by A.q_id ) I group by q_id
答案 3 :(得分:0)
MySQL解决方案(sql fiddle):
SELECT tmp.q_id, MIN(c) as correct
FROM (
SELECT qa.q_id, IF(qa.q_id = usa.q_id, 1, 0) as c
FROM question_answers qa
LEFT JOIN user_submited_answers usa
ON qa.q_id = usa.q_id AND qa.ans_id = usa.sub_ans_id
UNION
SELECT usa.q_id, IF(qa.q_id = usa.q_id, 1, 0) as c
FROM question_answers qa
RIGHT JOIN user_submited_answers usa
ON qa.q_id = usa.q_id AND qa.ans_id = usa.sub_ans_id
) tmp
GROUP BY tmp.q_id;
现在,一步一步解释:
为了获得正确的输出,我们需要:
question_answers
表格中提取用户未填写的答案(在您的示例中为q_id = 3
ans_id = 3
)user_submited_answers
表格中提取用户填写的错误答案(在您的示例中为q_id = 3
sub_ans_id = 4
)为此,我们可以使用完全外连接(对于mysql左连接+右连接):
SELECT *
FROM question_answers qa
LEFT JOIN user_submited_answers usa
ON qa.q_id = usa.q_id AND qa.ans_id = usa.sub_ans_id
UNION
SELECT *
FROM question_answers qa
RIGHT JOIN user_submited_answers usa
ON qa.q_id = usa.q_id AND qa.ans_id = usa.sub_ans_id;
从前面的查询结果中,我们要查找的行(错误答案)包含NULL值(基于案例,在question_answers
表或user_submited_answers
表中)。
下一步是使用IF或CASE语句将这些行标记为0(错误答案):IF(qa.q_id = usa.q_id, 1, 0)
。
要获得最终输出,我们需要按q_id
进行分组,并在分组行中查找0个值。如果至少有一个0,则该问题的答案是错误的,应将其标记为。
检查sql小提琴:SQL Fiddle