如何优化此SQL查询?
Private Sub cmb1_AfterUpdate()
Me.cmb2.RowSource = ""
Me.cmb2.Value = ""
Me.cmb3.RowSource = ""
Me.cmb3.Value = ""
Dim strSQL As String
If IsNull(cmb1) = False Then
strSQL = "Select ID_Group " & _
"From Tbl_Mst_Group " & _
"Where ID_License = '" & Me.cmb1 & "'"
Me.cmb2.RowSource = strSQL
Me.cmb2.Requery
Else
Me.cmb2.RowSource = ""
Me.cmb2.Value = ""
Me.cmb3.RowSource = ""
Me.cmb3.Value = ""
End If
Private Sub cmb2_AfterUpdate()
Dim strSQL As String
If IsNull(cmb2) = False Then
strSQL = "Select ID_SubGroup " & _
"From Tbl_Mst_SubGroup " & _
"Where ID_Group = '" & Me.cmb2 & "'"
Me.cmb3.RowSource = strSQL
Me.cmb3.Requery
Else
Me.cmb3.RowSource = ""
Me.cmb3.Value = ""
End If
End Sub
如果有一种方法可以减少“ where”条件的写入,那就没问题了。
答案 0 :(得分:1)
首先,WHERE
子句中的三个条件实际上是联接条件的一部分,并且应该正确地出现在ON
子句中,并带有现代显式联接。考虑以下版本:
SELECT
ec.code_element,
ec.code_caracteristique,
ec.valeur_texte,
ec.valeur_num,
ec.valeur_date,
ec.index_tableau,
ec.valeur_liste,
e.code_type_element
FROM ods_element_caracteristique ec
INNER JOIN ods_element e
ON e.code = ec.code_element
INNER JOIN ods_caracteristique c
ON c.name = ec.code_caracteristique
INNER JOIN ods_modele_caracteristique mc
ON c.idmodele_caracteristique = mc.id
WHERE
mc.idtype_valeur != 6 AND
ec.valeur_liste IS NULL AND
ec.code_caracteristique != 'TE_GENER_DATE_MES';
通过升级到现代联接语法,它还减小了WHERE
子句的大小。
答案 1 :(得分:0)
以较少的“ where”条件写出来
您是说缩短它而不改变它的含义吗?
WHERE
(e.code, c.name, c.idmodele_caracteristique) = (ec.code_element, ec.code_caracteristique, mc.id)
AND (mc.idtype_valeur, ec.code_caracteristique) != (6, 'TE_GENER_DATE_MES')