2011-04-28 59 views
1

我有两个SQL表,包括AttendanceID,StudentID,ModuleID,Present和Date字段的考勤表。另一个表是学生表,它具有StudentID字段和名称字段。 我想生成一个SQL语句,它从考勤表中选择AttendanceID,StudentID,ModuleID,Present和Date,但也根据在Textbox控件中输入的StudentID选择Student Table中的Name字段。 任何人都可以帮助我实现这一点,我想我需要一个子查询,但我不知道如何做到这一点,因为我只是一个初学MySQL的人。 这是我迄今为止的代码,用于选择考勤表中的所有字段,但不根据所选StudentID从学生表中选择姓名。选择语句以根据在文本框中输入的ID从两个表中选择记录

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" 
        SelectCommand="SELECT * FROM [Attendance] WHERE ([StudentID] = @StudentID)"> 
        <SelectParameters> 
         <asp:ControlParameter ControlID="pnumTextBox" Name="StudentID" 
          PropertyName="Text" Type="String" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 

在此先感谢!

回答

1

您需要加入StudentAttendance表以获取该信息。这是一个可以做到这一点的查询。

SELECT AttendanceID, Student.StudentID, ModuleID, Present, Date, Name 
FROM Attendance, Student 
WHERE Attendance.StudentID = Student.StudentID 
AND (Student.StudentID = @StudentID) 
+0

StudentID不明确,因为它存在于两个表中。更改提供的select语句@Ravi,以使用正确的TableName为所有选定的列加前缀。即SELECT Student.AttendanceID,Student.StudentID,...等。或者,为了便于阅读,请使用[表别名](http://www.java2s.com/Tutorial/SQLServer/0020__Query/Tablealias.htm) – Smudge202 2011-04-28 10:53:21