0

我有一张桌子,上面有一个工作篮子的工作。如果您没有以用户身份登录,那么您的作业会从 cookie 中获取 sessionid。

如果您在之后登录,并且您的已注销购物篮和已登录购物篮中已有工作,则将它们与您的 id 一起添加并将 session_id 设置为 0。

该表如下所示:

vacancy_id  |  user_id  |  session_id

  10            1            0

  10            0            1636252

  4             0            1636252

因此,当我运行 SQL 时,ID 为 10 和会话 ID 的空缺将被删除,ID 为 4 和会话 ID 的空缺将具有会话 ID 0 和用户 ID 1。

我确实在下面尝试过,但我收到一条错误消息

“您不能在 FROM 子句中指定目标表 'v1' 进行更新”

代码:

            Dim current_sessionid As String = GetCookie("job_session_id")
            If String.IsNullOrEmpty(current_sessionid) Then
            Else

                Dim oConn3 As New MySqlConnection(DSN)
                Dim oCmd3 As New MySqlCommand
                Dim oRS3 As MySqlDataReader = Nothing

                Dim oConn4 As New MySqlConnection(DSN)
                Dim oCmd4 As New MySqlCommand
                Dim oRS4 As MySqlDataReader = Nothing

                Try

                    oConn3.Open()

                    oCmd3.Connection = oConn

                    oCmd3.CommandText = "UPDATE vacancies_basket AS v1 SET v1.user_id = @user_id, v1.session_id = '0' WHERE session_id = @currentsession AND " _
                    & "(SELECT v2.vacancy_id FROM vacancies_basket AS v2 WHERE user_id = @user_id) <> v1.vacancy_id"
                    oCmd3.Prepare()
                    oCmd3.Parameters.AddWithValue("@user_id", User.User_ID)
                    oCmd3.Parameters.AddWithValue("@currentsession", GetCookie("job_session_id"))
                    oCmd3.ExecuteNonQuery()

                    oConn3.Close()
                    oConn3.Dispose()
                    oCmd3.Dispose()
                    oRS3 = Nothing

                    oCmd4.CommandText = "DELETE FROM vacancies_basket WHERE session_id=@currentsession"
                    oCmd4.Prepare()
                    oCmd4.Parameters.AddWithValue("@currentsession", GetCookie("job_session_id"))
                    oCmd4.ExecuteNonQuery()

                Catch ex As Exception
                    SendAlertEMail("App_Code/functions.vb/LogUserId()/jobbasket", ex)
                Finally
                    oConn4.Close()
                    oConn4.Dispose()
                    oCmd4.Dispose()
                    oRS4 = Nothing
                End Try
            End If
4

0 回答 0