选择Statement以根据在Textbox中输入的ID从两个表中选择记录

问题描述 投票:1回答:1

我有两个SQL表,一个出勤表,其中包含AttendanceID,StudentID,ModuleID,Present和Date字段。另一个表是Student Table,它具有StudentID字段和Name字段。我想生成一个SQL语句,它从出勤表中选择AttendanceID,StudentID,ModuleID,Present和Date,但也根据在Textbox Control中输入的StudentID选择Student Table中的Name字段。任何人都可以帮我用SQL实现这一点,我想我需要一个子查询,但我不知道如何做到这一点,因为我只是MySQL的初学者。到目前为止,这是我的代码,它选择考勤表中的所有字段,但不会根据所选的学生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>

提前致谢!

asp.net mysql subquery
1个回答
1
投票

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

SELECT AttendanceID, Student.StudentID, ModuleID, Present, Date, Name 
FROM Attendance, Student 
WHERE Attendance.StudentID = Student.StudentID 
AND (Student.StudentID = @StudentID)
© www.soinside.com 2019 - 2024. All rights reserved.