如何在单个查询中执行以下操作?
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;
答案 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;