我通过SQL语句进行了查询。我得到了正确的结果,每个结果在查询中重复4次。我很困惑为什么我得到4份相同的结果。
以下是表单的代码:
Dim strTables As String
Private Sub btnFollowUpQs_Click()
If (btnFollowUpQs.Value = True) Then
Set db = CurrentDb()
Set rs = db.OpenRecordset(btnFollowUpQs.Caption)
Dim fld As DAO.Field
For Each fld In rs.Fields
Me.lstVariablesFollowUpQs.AddItem (fld.Name)
Next
Set fld = Nothing
db.Close
strTables = strTables + "," + btnFollowUpQs.Caption
Else
lstVariablesFollowUpQs.RowSource = ""
strTables = Replace(strTables, "," + btnFollowUpQs.Caption, "")
End If
Debug.Print strTables
End Sub
Private Sub btnBaseline_Click()
If (btnBaseline.Value = True) Then
Set db = CurrentDb()
Set rs = db.OpenRecordset(btnBaseline.Caption)
Dim fld As DAO.Field
For Each fld In rs.Fields
Me.lstVariablesBaseline.AddItem (fld.Name)
Next
Set fld = Nothing
db.Close
strTables = strTables + "," + btnBaseline.Caption
Else
lstVariablesBaseline.RowSource = ""
strTables = Replace(strTables, "," + btnBaseline.Caption, "")
End If
Debug.Print strTables
End Sub
Private Sub btnTreatments_Click()
If (btnTreatments.Value = True) Then
Set db = CurrentDb()
Set rs = db.OpenRecordset(btnTreatments.Caption)
Dim fld As DAO.Field
For Each fld In rs.Fields
Me.lstVariablesTreatments.AddItem (fld.Name)
Next
Set fld = Nothing
db.Close
strTables = strTables + "," + btnTreatments.Caption
Else
lstVariablesTreatments.RowSource = ""
strTables = Replace(strTables, "," + btnTreatments.Caption, "")
End If
Debug.Print strTables
End Sub
Private Sub btnQuestionnaires_Click()
If (btnQuestionnaires.Value = True) Then
Set db = CurrentDb()
Set rs = db.OpenRecordset(btnQuestionnaires.Caption)
Dim fld As DAO.Field
For Each fld In rs.Fields
Me.lstVariablesQuestionnaires.AddItem (fld.Name)
Next
Set fld = Nothing
db.Close
strTables = strTables + "," + btnQuestionnaires.Caption
Else
lstVariablesQuestionnaires.RowSource = ""
strTables = Replace(strTables, "," + btnQuestionnaires.Caption, "")
End If
Debug.Print strTables
End Sub
Private Function createSQL(ByRef lstCtrl As Control, v() As String) As String
Dim count As Integer
count = 0
With lstCtrl
For Each varSelected In .ItemsSelected
If Not IsNull(varSelected) Then
Dim sel As String
sel = (lstCtrl.Column(0, varSelected))
strSQL = strSQL + sel & " " & v(count) & " AND "
End If
count = count + 1
Next
End With
createSQL = strSQL
End Function
Private Sub btnBuildQuery_Click()
If Left(strTables, 1) = "," Then
strTables = Right(strTables, Len(strTables) - 1)
End If
Dim tables() As String
tables = Split(strTables, ",")
Dim strSQL As Variant
strSQL = "SELECT * FROM " & strTables & " WHERE "
For Each Table In tables
Dim values() As String
Select Case Table
Case "tblPatientHistoryBaseline"
values = Split(txtSearchValueBaseline, ",")
strSQL = strSQL + createSQL(lstVariablesBaseline, values)
Case "tblQuestionnaires"
values = Split(txtSearchValueQuestionnaires, ",")
strSQL = strSQL + createSQL(lstVariablesQuestionnaires, values)
Case "tblTreatments"
values = Split(txtSearchValueTreatments, ",")
strSQL = strSQL + createSQL(lstVariablesTreatments, values)
Case "tblFollowUpQs"
values = Split(txtSearchValueFollowUpQs, ",")
strSQL = strSQL + createSQL(lstVariablesFollowUpQs, values)
End Select
Next
strSQL = Left(strSQL, Len(strSQL) - 5)
Debug.Print (strSQL)
Dim qdf As QueryDef
Set qdf = CurrentDb.CreateQueryDef("qry" & txtQueryName, strSQL)
DoCmd.OpenQuery qdf.Name
End Sub
以下是查询返回的内容:
LastName FirstName ID Visit
Line Georgia 1234567 0
Line Georgia 1234567 0
Line Georgia 1234567 0
Line Georgia 1234567 0
Doe Jane 0123456 0
Doe Jane 0123456 0
Doe Jane 0123456 0
Doe Jane 0123456 0
以下是我生成的示例SQL:
SELECT * FROM tblQuestionnaires, tblPatientHistoryBaseline WHERE Visit = 0 AND LastName Like '*e*'
我猜测我的SQL有问题,但我无法弄清楚是什么。
谢谢!
“EDIT ----------------- LastName来自tblPatientHistoryBaseline, 访问来自tblQuestionnaires
答案 0 :(得分:3)
您没有说明tblQuestionnaires
与tblPatientHistoryBaseline
的关系?一个合适的JOIN
可能会解决这个问题:
SELECT *
FROM tblQuestionnaires AS q
INNER JOIN tblPatientHistoryBaseline AS ph ON q.ID = ph.ID
WHERE Visit = 0
AND LastName Like '*e*'
我不太确定哪个字段来自您的选择。如果任一表中有多个记录,则上述查询仍可能产生重复。如果是这种情况,您可以将DISTINCT语句添加到查询中并指定所需的字段:
SELECT DISTINCT LastName, FirstName, ph.ID, Visit
FROM tblQuestionnaires AS q
INNER JOIN tblPatientHistoryBaseline AS ph ON q.ID = ph.ID
WHERE Visit = 0
AND LastName Like '*e*'
<强> 编辑: 强>
SELECT DISTINCT ph.ID, q.fieldname, ph.fieldname, t.fieldname, f.fieldname
FROM tblQuestionnaires AS q
INNER JOIN tblPatientHistoryBaseline AS ph ON q.ID = ph.ID
INNER JOIN tblTreatments AS t ON t.ID = ph.ID
INNER JOIN tblFollowUp AS f ON f.ID = ph.ID
WHERE Visit = 0
AND LastName Like '*e*'
答案 1 :(得分:1)
在这种情况下,加入它们是强制性的。例如,应编写代码以仅指定问卷表中的匹配行。也就是说,
SELECT *
FROM tblQuestionnaires, tblPatientHistoryBaseline
WHERE Visit = 0
AND LastName Like '*e*'
AND tblQuestionnaires.id = tblPatientHistoryBaseline.id
但我也建议使用更新的(过去十年)语法:
SELECT *
FROM tblQuestionnaires q
INNER JOIN tblPatientHistoryBaseline b
ON q.id = b.id
WHERE q.Visit = 0
AND b.LastName Like '*e*'
我不确定您的VISIT列或LASTNAME列的来源,但如果您使用正确的别名作为前缀,则一切都会没问题。