我在SQL Server 2012中有两个表
的数据库第一个表格列是:
isSymbolicLink
第二个表格列是:
| Student_ID , Subject_ID , Subject_score , Subject_mark |
|--------------------------------------------------------|
| 1 | 1 | 92 | ? |
| 1 | 2 | 88 | ? |
|____________|____________|_______________|______________|
我想编写一个查询以从第二个表中获取 |Score_ID | Subject_mark |
|________________________|
| 100 | A+ |
| 95 | A |
| 90 | B+ |
| 85 | B |
| 80 | c |
| 75 | E |
|_________|______________|
并将其放在Subject_mark
的第一个表中
Subject_mark
答案 0 :(得分:1)
我正在打电话,所以我稍后要检查一下,因为它似乎有些不对劲,但尝试类似
Update table_1
Set Subject_mark = (select Subject_mark from table_2
where Score_ID <= (select Subject_score)
and Score_ID > (select Subject_score - 5))
使用给定的样本数据执行示例:
DECLARE @FirstTable TABLE (Student_ID INT, Subject_ID INT, Subject_score INT, Subject_mark VARCHAR(3))
INSERT INTO @FirstTable (Student_ID, Subject_ID, Subject_score, Subject_mark)
VALUES
(1, 1, 92, NULL),
(1, 2, 88, NULL)
DECLARE @SecondTable TABLE (Score_ID INT, Subject_mark VARCHAR(3))
INSERT INTO @SecondTable (Score_ID, Subject_mark)
VALUES
(100, 'A+'),
(95 , 'A'),
(90 , 'B+'),
(85 , 'B'),
(80 , 'C'),
(75 , 'E')
UPDATE @FirstTable
SET Subject_mark = (SELECT Subject_mark
FROM @SecondTable
WHERE Score_ID <= (SELECT Subject_score) AND
Score_ID > (SELECT Subject_score - 5))
SELECT * FROM @FirstTable
答案 1 :(得分:0)
鉴于商标被分类为每个5分的差异,这应该有效。
UPDATE t1
SET t1.Subject_mark = CASE WHEN t1.Subject_score < 75 THEN 'E' ELSE t2.Subject_mark END
FROM FirstTable t1
LEFT OUTER JOIN SecondTable t2 ON t1.Subject_score <= t2.Score_ID AND t1.Subject_score > t2.Score_ID - 5;