简体   繁体   English

更新连接查询不更新表中的记录

[英]Update Join Query not Updating Record in The Table

In my attendance table I want to update a record with S_ID and C_ID from StudentCourse bridge table but its not updating. 在我的考勤表中,我想从StudentCourse桥表更新带有S_IDC_ID的记录,但不更新。

https://imgur.com/a/7ZBItur https://imgur.com/a/7ZBItur

Its working when I use it to select and display 2 columns from the StudentCourse table and 1 column from the Attendance table but it doesn't work when I use it to update the attendance table which for now is empty 当我用它来选择和显示StudentCourse表中的2列和Attendance表中的1列时,它工作但当我用它来更新现在为空的考勤表时它不起作用

    UPDATE Attendance
    SET S_ID = sc.S_ID,
    C_ID = sc.C_ID
    FROM Attendance a
    left outer join StudentCourse sc ON a.S_ID = sc.S_ID
    WHERE sc.S_ID=2 and sc.C_ID=2

在此输入图像描述

There are two actually four tables: 实际上有两个表:

  • Student(S_ID(primary key)) 学生(S_ID(主键))
  • Course(C_ID(Primary key)) 课程(C_ID(主键))
  • StudentCourse((S_ID,C_ID(Foreign keys))Bridge table) StudentCourse((S_ID,C_ID(外键))桥表)
  • and attendance table with (S_ID,C_ID(Foreign keys)) 和考勤表(S_ID,C_ID(外键))

What I am doing is displaying data on datagridview by joining tables and selecting S_ID , S_Name , C_ID and Pre_Abs ( Attendance table column)columns from all these tables. 我正在做的是通过连接表并从所有这些表中选择S_IDS_NameC_IDPre_AbsAttendance表列)列来显示datagridview上的数据。

Now, I want to insert the info present in datagridview to the attendance table when I click on button. 现在,我想在单击按钮时将datagridview的信息插入到考勤表中。

I have done this already with simple insert query to attendance table by using datagrdview.rows[i].cell[2] property. 我已经通过使用datagrdview.rows[i].cell[2]属性对出勤表进行简单的插入查询。

I want to know if there is any better idea to do this so that I can use JOIN instead of using datagridview property with for loop. 我想知道是否有更好的想法这样做,以便我可以使用JOIN而不是使用带for循环的datagridview属性。

For now my attendance table is empty while Student , Course , and StudentCourse tables are filled with the data. 现在虽然我的出勤表是空的StudentCourseStudentCourse表中填充数据。

What I want is to display record( S_ID , C_ID ) from studentCourse table and ( Pre_Abs ) from Attendance table and when I submit the attendance; 我想要的是显示来自studentCourse表的记录( S_IDC_ID )和来自Attendance表的( Pre_Abs )以及当我提交出勤时; I want it to store Pre_abs record against each S_ID , C_ID in the attendance table. 我希望它在出勤表中针对每个S_IDC_ID存储Pre_abs记录。

在此输入图像描述

I don't think I can explain it any further. 我认为我不能再解释了。

You have declared alias for the table Attendance. 您已为表Attendance声明了别名。 So you should use alias reference before the column name as the same column name also available in other table. 因此,您应该在列名之前使用别名引用作为其他表中也可用的相同列名。 Can you please try this- 你可以试试这个 -

UPDATE a
SET a.S_ID = sc.S_ID,
a.C_ID = sc.C_ID
FROM Attendance a
LEFT OUTER JOIN StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2

I have also doubt about you where condition as you are Update table Attendance but filtering records on table StudentCourse. 我也怀疑你在哪里条件更新表出勤但过滤记录表StudentCourse。 use of wrong filter can resultant in update all rows in table Attendance. 使用错误的过滤器可以导致更新表中的所有行出席。 Please be careful. 请小心。

Hmmm. 嗯。 I do not understand the reason why your Attendance table exists in the first place. 我不明白你的Attendance表首先存在的原因。 Your StudentCourse table already seems to contain info about which students are having which courses. 您的StudentCourse表似乎已包含有关哪些学生正在学习哪些课程的信息。 I'll just assume the Attendance table has to deal with registering student attendance for each single lecture/lesson within a course. 我只是假设Attendance表必须处理在课程中为每个单一讲座/课程注册学生出勤率。

Anyway, as @Psi already commented, you seem to initially want an INSERT query to create a record in the Attendance table with data based on information in the StudentCourse table. 无论如何,正如@Psi已经评论过,您似乎最初想要一个INSERT查询在Attendance表中创建一个记录,其中包含基于StudentCourse表中信息的数据。 I guess that you are thinking about it too hard. 我猜你在考虑它太难了。 ;-) You may try something like this: ;-)你可以尝试这样的事情:

INSERT INTO Attendance (S_ID, C_ID, Pre_Abs)
SELECT S_ID, C_ID, Pre_Abs
FROM StudentCourse
WHERE S_ID = 2 AND C_ID = 2

However, it is currently unclear how field Pre_Abs should be filled... You try to get it from table Attendance , but that would seem to be invalid. 但是,目前还不清楚应该如何填写字段Pre_Abs ...你试图从表Attendance ,但这似乎是无效的。

Also, when creating INSERT-queries, make sure that the source data values (in the SELECT or VALUES clause) are in the same order as the target fields (in the INSERT clause). 此外,在创建INSERT查询时,请确保源数据值(在SELECT或VALUES子句中)与目标字段的顺序相同(在INSERT子句中)。 If the field/value order doesn't match, the data gets mixed up (if the query does not fail due to data type errors)! 如果字段/值顺序不匹配,则数据会混淆(如果查询因数据类型错误而未失败)!

And last but not least: if both Attendance and StudentCourse tables share the same composite key (S_ID, C_ID) , you should use that full key when joining related records of those tables: 最后但并非最不重要:如果AttendanceStudentCourse表共享相同的复合键(S_ID, C_ID) ,则在加入这些表的相关记录时应使用该完整键:

FROM
    Attendance AS A
    LEFT JOIN StudentCourse AS SC ON
        SC.S_ID = A.S_ID AND
        SC.C_ID = A.C_ID

Hope this helps a little... 希望这有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM