从另一个表中选择时从情况中选择

时间:2019-05-07 13:48:48

标签: php mysql

我有两个表,一个是表,其中包含以下结构enter image description here

表2中包含以下数据enter image description here

在示例(CASE WHEN SUBSTR(A.total, 1, 2) >= 70 THEN "A" WHEN SUBSTR(A.total, 1, 2) >= 60 THEN "B"  WHEN SUBSTR(A.total, 1, 2) >= 50 THEN "C" WHEN SUBSTR(A.total, 1, 2) >= 40 THEN "D" WHEN SUBSTR(A.total, 1, 2) <= 39 THEN "F"  END)

下,如何使用sql case来旋转表一?

这是完整的查询

INSERT INTO promotion_result (adm, subject, first, second, third, session, class, total, grade) SELECT DISTINCT a.admission_no, a.subject, Ifnull((SELECT b.total FROM result b WHERE b.admission_no = a.admission_no AND a.subject = b.subject AND b.term = 'First'), 0) AS first, Ifnull((SELECT b.total FROM result b WHERE b.admission_no = a.admission_no AND a.subject = b.subject AND b.term = 'Second'), 0) AS second, Ifnull((SELECT b.total FROM result b WHERE b.admission_no = a.admission_no AND a.subject = b.subject AND b.term = 'Third'), 0) AS third, '2018/2019', 'JS2', Sum(a.total / 3) AS total, CASE WHEN a.total >= B.mark THEN 'A' ELSE 'F' END FROM result a INNER JOIN grade B ON Substr(a.class, 1, 2) = B.class WHERE a.session = '2018/2019' AND a.class = 'JS2' AND a.admission_no = 'JS177' GROUP BY a.admission_no

0 个答案:

没有答案