【问题标题】:mysql update statement using 3 tablesmysql更新语句使用3个表
【发布时间】:2020-03-12 13:24:12
【问题描述】:

我正在开发一个在线测验管理数据库

  • 一个表 stdinfo 存储用户名和学生详细信息

  • 表 testinfo 以多行的形式存储 testid 名称主题及其标记方案(每个主题单独),如

  • 一桌问题有所有的问题和他们的qids

  • 一张表记录学生的回答

现在答案中有一个标记列,显示在该问题中获得的分数。

因为我想生成结果,所以我已经插入了响应及其 ID/用户名。现在我想为计算结果插入标记;

应该怎么做

  • 在响应为空的情况下设置标记=0;
  • 如果响应正确,则设置标记=从相应主题的 testinfo 表中获取的 posmark
  • 如果响应不正确,则设置从相应主题的 testinfo 表中获取的标记=negmark

表结构如下;

【问题讨论】:

  • 为什么整个问题都被格式化为引号?
  • 你为什么将文字作为图片发布?请在代码块中使用纯文本。

标签: mysql join sql-update


【解决方案1】:

查看您问题的答案 [此处][1] https://forums.mysql.com/read.php?20,85813,85816#msg-85816

或者看看这个:

UPDATE a
INNER JOIN b USING (id)
SET a.firstname='Pekka', a.lastname='Kuronen',
b.companyname='Suomi Oy',companyaddress='Mannerheimtie 123, Helsinki Suomi'
WHERE a.id=1;

【讨论】:

    【解决方案2】:

    自从我以前的答案被删除以来,我正在以更详细的方式重新发布答案。

    我自己找到了它,也发现@Jirka49 的答案很有帮助。请参阅下面的链接。

    See the last section in the webpage

    所以,我们需要避免使用 IN 运算符。

    因此在查询期间创建一个临时表,其中包含学生正确回答的问题的详细信息、测试信息和问题信息(正分)。

    现在使用此表通过 where 子句更新响应表。

    所以查询变成:

      update responses,
    (select posmark,responses.testid,responses.qid,responses.stid from testinfo,responses,question
     where testinfo.testid=responses.testid and responses.testid="<whatever testid>" and question.qid=responses.qid and question.cans=responses.response and question.subcode=testinfo.subcode and responses.stid='<for whichever student>')as temp
     set responses.marks=temp.posmark where responses.qid=temp.qid and temp.testid=responses.testid and responses.stid=temp.stid;
    

    对于负分也可以做类似的事情。并检查无论答案是否为空,一个简单的查询都会分配零分。

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2012-08-29
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      相关资源
      最近更新 更多