資料庫組合查詢

2021-06-18 14:50:55 字數 3919 閱讀 6525

在使用資料庫的過程中,資料的查詢是使用最多的,所以,資料的精確查詢是乙個很重要的問題。以前的資料查詢是最簡單的資料查詢,也從來沒想過組合查詢的問題,可是在做機房收費系統的時候,遇到了乙個很大的問題,當三個條件隨意組合的時候,會出現7中組合情況。具體問題,見下圖:

首先,當連線資料庫時,必須要用到查詢條件,即where子句,可是資料庫中的欄位名和窗體中的欄位名不相符,所以首先應該將窗體中的欄位名設定為資料庫可以識別的欄位名,**如下:

dim strname(2) as string

dim strrelation(2) as string

for index = 0 to 2

'資料庫不能識別窗體中的名稱,利用迴圈和選擇變成資料庫可以識別的名稱。

select case comboname(index).text

case "卡號"

strname(index) = "cardno"

case "學號"

strname(index) = "studentno"

case "姓名"

strname(index) = "studentname"

case "性別"

strname(index) = "***"

case "系別"

strname(index) = "department"

case "年級"

strname(index) = "grade"

case "班號"

strname(index) = "class"

end select

select case comborelation(index).text

case "與"

strrelation(index) = "and"

case "或"

strrelation(index) = "or"

end select

next

dim dd(2) as boolean

private sub cmdcheck_click()

msflexgrid.clear

if testtxt(comboname(0).text) = true then '判斷第乙個條件是否被使用

if testtxt(combosign(0).text) = true then

if testtxt(txtcontent1.text) = true then

dd(0) = true '標記第乙個條件

end if

end if

end if

if testtxt(comboname(1).text) = true then '判斷第二個條件是否被使用

if testtxt(combosign(1).text) = true then

if testtxt(txtcontent2.text) = true then

dd(1) = true

end if

end if

end if

if testtxt(comboname(2).text) = true then '判斷第三個條件是否被使用

if testtxt(combosign(2).text) = true then

if testtxt(txtcontent3.text) = true then

dd(2) = true

end if

end if

end if

if dd(0) = true then '首先判斷第乙個條件是否被選中,如果被選中,則補充完整查詢限制條件 txtsql = txtsql & " " & strname(0) & " " & combosign(0).text & " " & txtcontent1.text & " " end if '(條件1) if dd(1) = true then '判斷第二個條件是否被選中 if dd(0) = true then ' 再判斷第乙個條件是否被選中 txtsql = txtsql & " " & strrelation(0) & " " & strname(1) & " " & combosign(1).text & " " & txtcontent2.text & " " else '(條件2,1) txtsql = txtsql & " " & strname(1) & " " & combosign(1).text & " " & txtcontent1.text & " " end if '(條件2) if dd(2) = true then '判斷第三個條件是否被選中 if dd(0) = true or dd(1) = true then '判斷前兩個條件是否被選中 txtsql = txtsql & " " & strrelation(1) & " " & strname(2) & " " & combosign(2).text & " " & txtcontent3.text & " " else '(條件3,1) 或 (條件3,2) 或 (條件3,2,1) txtsql = txtsql & " " & strname(2) & " " & combosign(2).text & " " & txtcontent3.text & " " end if '條件(3) end if
if dd(0) = false and dd(1) = false and dd(2) = false then     '判斷是否選擇查詢條件

msgbox "您還沒有完整的選擇乙個查詢條件,請選擇查詢條件。", vbokonly + vbexclamation, "提示"

exit sub

comboname(0).setfocus

end if

set mrc = executesql(txtsql, msgtext)

if mrc.eof = true then

msgbox "沒有找到相關記錄,請重新輸入查詢條件。", vbokonly + vbexclamation, "提示"

exit sub

else

with msflexgrid '將查詢到的資料寫到表中

for i = 0 to mrc.recordcount - 1

.rows = .rows + 1

.textmatrix(.rows - 1, 0) = mrc.fields(0)

.textmatrix(.rows - 1, 1) = mrc.fields(1)

.textmatrix(.rows - 1, 2) = mrc.fields(2)

.textmatrix(.rows - 1, 3) = mrc.fields(3)

.textmatrix(.rows - 1, 4) = mrc.fields(4)

.textmatrix(.rows - 1, 5) = mrc.fields(5)

.textmatrix(.rows - 1, 6) = mrc.fields(6)

next

end with

end if

mrc.close

end sub

首先用dd(2)用來標記三個查詢條件,中間的**用來選擇查詢方式,三個查詢條件組合可以組合成七種組合,也可以把所有的組合方式都寫出來,但是,我們採用了一種更簡便的方法,就是不管組合方式,我們只考慮單個的查詢條件,如果選中第乙個條件,則把第乙個條件寫入記憶體,如果第二個條件被選中,則把第二個條件寫入記憶體中,依次類推。

需要說明的是,每乙個查詢子句並不是完整的查詢語句,都是跟上邊查詢語句的結合而形成的。這樣可以減少**的重複編寫。

最後利用迴圈語句,將查詢到的結果寫到**中。

MySQL資料庫 組合查詢

這一次說下如何利用union操作符實現組合查詢,即將多條select語句組合成乙個結果集。我們目前每次使用資料庫查詢都是用單個select來用的,但是mysql也允許執行多個查詢,就是說多個select,但是是乙個結果集。這樣的組合查詢稱為並 union 也叫復合查詢。有兩種情況,我們需要用到組合查...

資料庫組合查詢和模糊查詢

搜尋條件 排序 得到所有捲菸 param is promote 0表示是 1表示捲菸列表 param pricevalues 排序 1 降序,2公升序 param price 批發價 預設為 不限 param cigarettefactory 菸廠 預設為 不限 param activity 活動 ...

Python資料庫查詢之組合條件查詢 F Q查詢

f查詢 取字段的值 關於查詢我們知道有 filter values get exclude 如果是聚合分組,還會用到aggregate和annotate,甚至還有萬能的雙下劃線,但是如果有這樣乙個需求,查詢a表中的aa欄位數值大於b表中bb欄位數值,應該怎麼做呢,django提供乙個f表示式來支援這...