基于其他两个表更新列

问题描述 投票:0回答:3

我有以下三个表如下,

select * from student_grade;
+--------------+----------+----------+-------+--------------------------+
| enrollmentid | courseid | personid | grade | credits_earned           |
+==============+==========+==========+=======+==========================+
|            1 |     1001 |        1 | A     |                     null |
|            2 |     1002 |        1 | B     |                     null |
|            3 |     1003 |        1 | A     |                     null |
|            4 |     3001 |        3 | A     |                     null |
|            5 |     3001 |        2 | B     |                     null |
|            6 |     4001 |        4 | A     |                     null |
|            7 |     4002 |        4 | A     |                     null |
+--------------+----------+----------+-------+--------------------------+
7 tuples
sql>select * from course;
+----------+-------------------+---------+--------------+
| courseid | title             | credits | departmentid |
+==========+===================+=========+==============+
|     1001 | Data structures   |      12 |          101 |
|     1002 | Algorithms        |      12 |          101 |
|     1003 | Graphics          |      20 |          101 |
|     2001 | DSP               |      20 |          102 |
|     2002 | Matlab            |      20 |          102 |
|     2003 | Maths             |      10 |          102 |
|     3001 | CAD               |      10 |          104 |
|     4001 | Power electronics |      10 |          103 |
|     4002 | Semi conductors   |      20 |          103 |
+----------+-------------------+---------+--------------+
9 tuples
sql>select * from grade_to_credits;
+-----------+--------------------------+
| gradechar | credits                  |
+===========+==========================+
| A         |                        1 |
| B         |                      0.9 |
+-----------+--------------------------+

我想要做的是:我正在使用credits_earned表的student_gradecredits表的course列更新credits表的grade_to_credits列。

像这样,

select c.credits * gc.credits 
from course c, grade_to_credits gc, student_grade sg 
where sg.courseid = c.courseid and sg.grade = gc.gradechar;
+--------------------------+
| L2                       |
+==========================+
|                       12 |
|                     10.8 |
|                       20 |
|                       10 |
|                        9 |
|                       10 |
|                       20 |
+--------------------------+

我在单独执行时获得上述值,但现在我想使用更新查询在student_grade表中更新这些值。

现在我使用的查询为:

update student_grade 
set credits_earned = (select c.credits * gc.credits 
                      from course c, grade_to_credits gc, student_grade sg 
                      where sg.courseid = c.courseid 
                        and sg.grade = gc.gradechar);

但上面的查询不起作用,我得到错误:

基数违规,预期标量值

我知道,我可以单独设置值,但我想从更新命令执行此操作。请纠正我出错的地方。

sql sql-update monetdb
3个回答
2
投票

无需过度复杂化使用LEFT JOIN来实现结果。设置操作通常比逐行操作更有效。

update student_grade 
SET  credits_earned = a.credits_earned
from (
select  s.enrollmentid,
        s.courseid,
        s.personid,
        s.grade,
        (g.credits * c.credits) as credits_earned
from student_grade s
LEFT OUTER JOIN @grade_to_credits g ON g.gradechar = s.grade
LEFT OUTER JOIN @course c           ON c.courseid  = s.courseid 
) as a
where student_grade.enrollmentid = a.enrollmentid

结果:

1   1001    1   A   12
2   1002    1   B   10,8
3   1003    1   A   20
4   3001    3   A   10
5   3001    2   B   9
6   4001    4   A   10
7   4002    4   A   20

1
投票

注意:下面的解决方案适用于MySQL。 (OP后来将问题从MySQL更改为MonetDB)

  • 尝试的问题是内部子查询返回多行。
  • 即使它只返回一行,它的另一个问题是更新表credits_earned中的所有student_grade字段具有相同的值(内部子查询的输出)。
  • 您应该避免使用隐式连接。他们老了,不是一个好习惯。

你应该更喜欢INNER JOIN这些表,然后使用连接表的列来更新行方向数据。请尝试以下方法:

update student_grade sg 
inner join course  c on c.courseid = sg.courseid 
inner join grade_to_credits gc on gc.gradechar = sg.grade 
set sg.credits_earned = c.credits * gc.credits;

0
投票

如果我理解正确,你只需要从子查询中删除student_grade

update student_grade st
    set credits_earned = (select sc.credits * gc.credits
                          from course c cross join
                               grade_to_credits gc
                          where sg.courseid = c.courseid and
                                sg.grade = gc.gradechar
                         );
© www.soinside.com 2019 - 2024. All rights reserved.