终于算是接触到机房里的重点加难点里的组合查询了,这个知识点说简单也简单,说难也难,关键是对他的理解到了哪一步。
什么是组合查询
大多数的SQL查询只包含从一个或多个表中返回数据的单条SELECT语句,但是,SQL也允许执行多个查询(多条SELECT语句),并将结果作为一个查询结果集返回。这些组合查询通常称为并或复合查询。
主要有两种情况需要使用组合查询:
(1)在一个查询中从不同的表返回结构数据
(2)对一个执行多个查询,按一个查询返回数据
首先是组合框列表的字段转换
Select Case Trim(comboField)
Case "卡号"
FileName = "cardno"
Case "姓名"
FileName = "studentname"
Case "上机日期"
FileName = "ondate"
Case "上机时间"
FileName = "ontime"
Case "下机日期"
FileName = "offdate"
Case "下机时间"
FileName = "offtime"
Case "消费金额"
FileName = "consume"
Case "金额"
FileName = "cash"
Case "备注"
FileName = "status"
End Select
End Function
还有就是要根据不同的内容选用不同的操作符
If combol(Index).Text = "姓名" Then
Symbol(Index).Clear
Symbol(Index).AddItem "="
Symbol(Index).AddItem "<>"
Else
Symbol(Index).Clear
Symbol(Index).AddItem "="
Symbol(Index).AddItem ">"
Symbol(Index).AddItem "<"
Symbol(Index).AddItem "<>"
End If
接下来就是组合关系
'第一组判断
'如果第一个字段名为空或者第一个操作符为空或内容为空,则显示msgbox中的内容,否则,退出程序
If Trim(comboField1.Text) = "" Or Trim(comboOpsign1.Text) = "" Or Trim(txtContent1.Text) = "" Then
MsgBox "请将第一行内容填写完整", 0, "温馨提示"
Exit Sub
Else
'将查询到的部分赋予到combo框中
txtsql = txtsql & " " & field(comboField1.Text) & " " & Trim(comboOpsign1.Text) & "'" & Trim(txtContent1.Text) & "'"
'第二组判断
If comboRelation1.Text <> "" Then
Select Case Trim(comboField2.Text)
'判断是否为日期型
Case "上机日期"
DTPicker2.MaxDate = Date
If Format(DTPicker2.Value, "yyyy-mm-dd") > Format(DTPicker2.MaxDate, "yyyy-mm-dd") Then
MsgBox "您选择的日期不能大于当前日期", 0 + 48, "提示"
Exit Sub
End If
txtContent2.Text = Format(DTPicker2.Value, "yyyy-mm-dd")
Case "下机日期"
DTPicker2.MaxDate = Date
If Format(DTPicker2.Value, "yyyy-mm-dd") > Format(DTPicker2.MaxDate, "yyyy-mm-dd") Then
MsgBox "您选择的日期不能大于当前日期", 0 + 48, "提示"
Exit Sub
End If
txtContent2.Text = Format(DTPicker2.Value, "yyyy-mm-dd")
'判断是否为时间类型
Case "上机时间"
txtContent2.Text = Format(DTPicker2.Value, "hh:mm:ss")
Case "下机时间"
txtContent2.Text = Format(DTPicker2.Value, "hh:mm:ss")
'为其他
If Trim(txtContent2.Text) = "" Then
txtContent2.SetFocus
End If
End Select
If Trim(comboField2.Text) = "" Or Trim(comboOpsign2.Text) = "" Or Trim(txtContent2.Text) = "" Then
MsgBox "请将第二行内容填写完整", 0, "温馨提示"
Exit Sub
Else
txtsql = txtsql & " " & RelationName(comboRelation1.Text) & " " & field(comboField2.Text) & " " & comboOpsign2.Text & "'" & Trim(txtContent2.Text) & "'"
'第三组判断
If comboRelation2.Text <> "" Then
Select Case Trim(comboField3.Text)
'判断是否为日期型
Case "上机日期"
DTPicker3.MaxDate = Date
If Format(DTPicker3.Value, "yyyy-mm-dd") > Format(DTPicker3.MaxDate, "yyyy-mm-dd") Then
MsgBox "您选择的日期不能大于当前日期", 0 + 48, "提示"
Exit Sub
End If
txtContent3.Text = Format(DTPicker3.Value, "yyyy-mm-dd")
Case "下机日期"
DTPicker3.MaxDate = Date
If Format(DTPicker3.Value, "yyyy-mm-dd") > Format(DTPicker3.MaxDate, "yyyy-mm-dd") Then
MsgBox "您选择的日期不能大于当前日期", 0 + 48, "提示"
Exit Sub
End If
txtContent3.Text = Format(DTPicker3.Value, "yyyy-mm-dd")
'判断是否为时间类型
Case "上机时间"
txtContent3.Text = Format(DTPicker3.Value, "hh:mm:ss")
Case "下机时间"
txtContent3.Text = Format(DTPicker3.Value, "hh:mm:ss")
'为其他
If Trim(txtContent3.Text) = "" Then
txtContent3.SetFocus
End If
End Select
If Trim(comboField3.Text) = "" Or Trim(comboOpsign3.Text) = "" Or Trim(txtContent3.Text) = "" Then
MsgBox "请将第三行内容填写完整", 0, "温馨提示"
Exit Sub
Else
txtsql = txtsql & " " & RelationName(comboRelation2.Text) & " " & field(comboField3.Text) & " " & comboOpsign3.Text & "'" & Trim(txtContent3.Text) & "'"
End If
End If
End If
End If
End If
---------------------