使用Equijoin更新查询

时间:2014-07-04 11:48:18

标签: mysql sql database jdbc

我有两张桌子。

学生 (包含以下列)

| StudentID |学生名称| FeeAccount |班级|

   01       Kid1          01      One     

   02       Kid2          02      Three

FeeAccount是表FeeAccounts中的外键。

我有桌子, FeeAccounts (其中包含以下列)

| FeeAccount | StudentID | MonthlyFee |欠款|

    01       01          --       --
    02       02          --       --

StudentID是表学生的外键。

====================的问题 =================== =====

我想在FeeAccounts表中更新 MonthlyFee 列的值,但必须为每个学生的班级添加特定费用。例如,对于第一类,我想将500添加到 monthlyfee ,我想将700添加到第三类的学生帐户。

  

其他词   我想根据学生班级更新每月课程,其中应使用外键获取学生班级,即学生ID。

我可以SELECT我所需的专栏,但我似乎不了解如何UPDATE使用 Equijoin

SELECT Query将是这样的:

SELECT f.feeaccount, f.studentname, s.class
FROM feeaccounts f, student s
WHERE f.studentid = s.studentid;

[[Btw我将在JDBC中使用查询,所以我希望你的帮助与JDBC兼容]]

2 个答案:

答案 0 :(得分:1)

documentation解释说有一个多表更新。它给出了一个通过提及另一个表来更新一个表的示例。 (RTM)。

UPDATE FeeAccount f, Student s
SET f.MonthlyFee = foo(...,f.Arrears,...,s.Class,...)
WHERE f.StudentId = s.StudentId;

答案 1 :(得分:0)

我相信,这是FeeAccounts表的正常更新,

UPDATE FeeAccounts set MonthlyFee = MonthlyFee + 500 where FeeAccount = 01; //For Class One
UPDATE FeeAccounts set MonthlyFee = MonthlyFee + 700 where FeeAccount = 03; //For Class Three

您需要根据FeeAccount值更新每个Class MonthlyFee。 这是你需要的,不是吗?

相关问题