比较两个变量行并删除vb.net中同一个表中的重复项

时间:2012-10-01 16:09:41

标签: sql vb.net

我有一张桌子可以为工作篮提供工作。如果您未以用户身份登录,那么您的作业将从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和session id的空位将具有会话ID 0和user_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

0 个答案:

没有答案