根据IN子句设置为布尔值

时间:2018-10-17 11:09:37

标签: mysql

如何在单个查询中执行以下操作?

UPDATE clients SET online=0 WHERE id NOT IN(4,5,8,10,12) AND id>=2 AND id<=15 AND parentId=123;
UPDATE clients SET online=1 WHERE id     IN(4,5,8,10,12) AND id>=2 AND id<=15 AND parentId=123;

1 个答案:

答案 0 :(得分:4)

您可以使用CASE .. WHEN语句:

If yearlabel.Text <> "" Then

        Select Case yearlabel.Text

            Case "Grade 11"

                If course_label.Text <> "" Then

                    Select Case course_label.Text

                        Case "STEM"

                            If semester_label.Text <> "" Then

                                Dim str As String
                                Dim classname As String

                                Select Case semester_label.Text

                                    Case "First Semester"
                                        classname = "STEM G11 FS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [STEM G11 FS]"

                                    Case "Second Semester"
                                        classname = "STEM G11 SS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [STEM G11 SS]"

                                End Select

                                con.Close()
                                con.Open()

                                cmd = New SqlClient.SqlCommand(str, con)
                                cmd.CommandText = str
                                cmd.Connection = con
                                drd = cmd.ExecuteReader
                                subject_cbx.Items.Clear()

                                While (drd.Read())
                                    subject_cbx.Items.Add(drd(classname))
                                End While

                                cmd.Dispose()
                                drd.Close()
                                con.Close()

                            End If

                        Case "HUMSS"

                            If semester_label.Text <> "" Then

                                Dim str As String
                                Dim classname As String

                                Select Case semester_label.Text

                                    Case "First Semester"
                                        classname = "HUMSS G11 FS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [HUMSS G11 FS]"

                                    Case "Second Semester"
                                        classname = "HUMSS G11 SS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [HUMSS G11 SS]"

                                End Select

                                con.Close()
                                con.Open()

                                cmd = New SqlClient.SqlCommand(str, con)
                                cmd.CommandText = str
                                cmd.Connection = con
                                drd = cmd.ExecuteReader
                                subject_cbx.Items.Clear()

                                While (drd.Read())
                                    subject_cbx.Items.Add(drd(classname))
                                End While

                                cmd.Dispose()
                                drd.Close()
                                con.Close()

                            End If

                    End Select

                End If

            Case "Grade 12"

                If course_label.Text <> "" Then

                    Select Case course_label.Text

                        Case "STEM"

                            If semester_label.Text <> "" Then

                                Dim str As String
                                Dim classname As String

                                Select Case semester_label.Text

                                    Case "First Semester"
                                        classname = "STEM G12 FS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [STEM G12 FS]"

                                    Case "Second Semester"
                                        classname = "STEM G12 SS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [STEM G12 SS]"

                                End Select

                                con.Close()
                                con.Open()

                                cmd = New SqlClient.SqlCommand(str, con)
                                cmd.CommandText = str
                                cmd.Connection = con
                                drd = cmd.ExecuteReader
                                subject_cbx.Items.Clear()

                                While (drd.Read())
                                    subject_cbx.Items.Add(drd(classname))
                                End While

                                cmd.Dispose()
                                drd.Close()
                                con.Close()

                            End If

                        Case "HUMSS"

                            If semester_label.Text <> "" Then

                                Dim str As String
                                Dim classname As String

                                Select Case semester_label.Text

                                    Case "First Semester"
                                        classname = "HUMSS G12 FS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [HUMSS G12 FS]"

                                    Case "Second Semester"
                                        classname = "HUMSS G12 SS"
                                        str = "SELECT* FROM [Subjects] ORDER BY [HUMSS G12 SS]"

                                End Select

                                con.Close()
                                con.Open()

                                cmd = New SqlClient.SqlCommand(str, con)
                                cmd.CommandText = str
                                cmd.Connection = con
                                drd = cmd.ExecuteReader
                                subject_cbx.Items.Clear()

                                While (drd.Read())
                                    subject_cbx.Items.Add(drd(classname))
                                End While

                                cmd.Dispose()
                                drd.Close()
                                con.Close()

                            End If

                    End Select

                End If

        End Select

    End If

IN(..)是逻辑/比较功能。因此,您也可以执行以下操作(仅在MySQL中 ):

UPDATE clients 
SET online = CASE WHEN id IN(4,5,8,10,12)
                    THEN 1 
                  ELSE 0 
             END
WHERE
    id BETWEEN 2 AND 15 AND parentId = 123;