0

我正在尝试创建等候名单。

这个等待名单背后的想法是,我们有一个培训问题,要求用户在课堂上注册一个座位。

最多可容纳 45 人。

如果所有 45 个座位​​都已被占用,则从 45 开始并随着课程注册而减少的 available_seating 将为 0。

如果available_seating 为0,则表示班级已满,没有座位。

当用户尝试报名参加可用座位 = 0 的课程时,我们希望让用户知道该课程已满,他/她将被列入等候名单。

我想我知道如何解决这个问题。

当 available_seating > 0 时,将用户插入 tblTrainings 表。

但是,如果 available_seating = 0 则将用户插入到名为 waitingList 的表中。

我的问题是如何将该条件语句与下面的计数器混合。

下面,SELECT Count(*) 查询检查用户是否已经注册了一个特定的类。

如果没有,请注册该课程的用户。如果是,请提醒用户他/她已经注册了该课程。

我如何将两者整合在一起,以便我们首先检查用户是否已经注册了该课程。

如果没有,那么我们检查类是否还没有满。如果已满,将用户插入waitingList。如果未满,则将用户插入 tblTrainings。

请参阅当前代码并提前非常感谢。

    'If user already registered for a class, alert user
    s = "SELECT Count(*) FROM tblTrainings WHERE Username = '" & username & "' AND CourseID = " & Request.QueryString("cosId") & "  AND LocationID = " & Request.QueryString("locid") & " AND dateId = " & Request.QueryString("iddate") & ""
    Dim connSt As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
    Dim connc As New SqlConnection(connSt)
    Dim cmdc As New SqlCommand(s, connc)
    connc.Open()
    cmdc.ExecuteNonQuery()
    counter = cmdc.ExecuteScalar()


    '   Now let's see if we found existing record
    If counter = 0 Then
        'User hasn't signed up for this class yet

        s = "INSERT INTO tblTrainings (CourseId, tblLocations.LocationId, dateId,AvailableSeats,username) VALUES (@CosID, @LocID, @dat, @Available_Seating, @Username)"
        Dim cmd = New SqlCommand(s, conn)
        cmd.Parameters.AddWithValue("@cosID", Request.QueryString("cosId"))
        cmd.Parameters.AddWithValue("@locID", Request.QueryString("locid"))
        cmd.Parameters.AddWithValue("@dat", Request.QueryString("iddate"))
        cmd.Parameters.AddWithValue("@Available_Seating", 45 - 1)
        cmd.Parameters.AddWithValue("@UserName", username)
        conn.Open()
4

2 回答 2

1

也许我错过了一些东西,但对我来说似乎并不复杂。

这个答案只使用重要的 SQL 片段来保持易读性,因为我主要指的是查询而不是 VB。

首先,你做你已经在做的事情——检查他们是否已经注册/在等待名单上。

SELECT Count(*) FROM (
   SELECT * FROM tblTrainings UNION SELECT * FROM waitingList
) AS t
WHERE Username = XXX AND CourseID = XXX AND LocationID = XXX AND dateID = XXX
/* Tells you whether the person is signed up already, whether for the course itself
or the waiting list. */
SELECT Count(*) FROM tblTrainings
WHERE CourseID = XXX AND LocationID = XXX AND dateID = XXX
-- This will give you the number of people in the course.

然后,一旦你有了它,你就可以决定是将它们添加到该表中还是将它们添加到等待列表中。

更新

也许这会有所帮助。

'Here build your querystring s from the SQL query above, filling in whatever values your need.
s = "SELECT Count(*) FROM (" & _
    "SELECT * FROM tblTrainings UNION SELECT * FROM waitingList" & _
    ") AS t" & _
    "WHERE Username = XXX AND CourseID = XXX AND LocationID = XXX AND dateID = XXX"    

Dim connSt As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
Dim connc As New SqlConnection(connSt)
Dim cmdc As New SqlCommand(s, connc)
connc.Open()
cmdc.ExecuteNonQuery()
counter = cmdc.ExecuteScalar()

If counter = 0 Then 'this means that the person is in neither the waitlist nor courselist
    
    'Fill in the XXX as necessary
    qs = "SELECT Count(*) FROM tblTrainings WHERE CourseID = XXX AND LocationID = XXX AND dateID = XXX"
    Set cmdc = New SqlCommand(s, connc)
    cmdc.ExecuteNonQuery()
    courseSize = cmdc.ExecuteScalar()

    If courseSize <= 45 Then
          'Insert the guy into the course
    Else
          'Insert the guy into waitList
    End If 
End If
于 2013-06-14T15:33:17.760 回答
0

假设您有一个 TableA,并且您只想在 TableB 计数为零时插入一条记录。然后你可以这样写:

insert into TableA (ColA, ColB) 
select 'a', 'b' from TableB 
 where 0 = (select count(*) 
              from TableB 
             where ColC = 'c')

将上述语句适合您的场景应该很容易。

于 2013-06-14T15:33:26.200 回答