我需要你的帮助,
是否有更有效的方法可以编写SQL语句来连接数据库并检索数值?我的逻辑方式就是这个。使用SQL Count来计算,将数值存储到var(类似于excel标记的单元格),然后使用vars并将结果输出到HTML表
我想知道是否有更有效的方式/方法,如果不是我在政治上对下面的方法是正确的吗?我猜多次连接数据库会慢一些吗?
我正在使用连接到MS Access数据库(mdb文件)的活动数据对象供我自己阅读。
var a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17
var b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17
var c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17
var d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17
var e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15,e16,e17
var f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17
var g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17
var h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17
var i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17
var j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15,j16,j17
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFAA' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
a3 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFAB' AND [DATE RECEIVED] BETWEEN #"+date1)+"# AND #"+date1_2+"#"
a4 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFAD' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
a5 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFBA' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
a7 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFBD' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
a8 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFBE' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
a9 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
DB_connect()
SQL = "SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFBM' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
a10 = Number(cn.Execute(SQL).Fields(0).Value)
cn.Close()
答案 0 :(得分:2)
您可以将它们全部合并到一个查询中:
SELECT
(SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFBA' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#") AS FirstColumn,
(SELECT COUNT(*) FROM tbl_imts WHERE [REQUEST TYPE]='MINISTERIAL DOCKET' AND [OPI]='ASFBD' AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#") AS SecondColumn,
Add other queries here...
FROM tbl_imts
这些是子查询,每个子查询返回一个可以分配给列的值,因此最终会得到一个查询,用于获取您正在查找的所有数据。
在SQL Server中,您不需要最终的FROM子句,但我认为在Access中您可能会这样做。
您可以像以前一样恢复您的价值观:
rst = cn.Execute(SQL)
a1 = rst.Fields("FirstColumn").Value
a2 = rst.Fields("SecondColumn").Value
将rst设置为DataSet或您想要使用的任何内容,然后将每个字段拉出来。
答案 1 :(得分:2)
我会从GROUP BY开始(我希望它适用于Access; - )
SELECT [OPI], COUNT(*) AS [CT]
FROM tbl_imts
WHERE [REQUEST TYPE]='MINISTERIAL DOCKET'
AND [OPI] IN ('ALL', 'POSSIBLE', ..) -- this line is optional
AND [DATE RECEIVED] BETWEEN #"+date1+"# AND #"+date1_2+"#"
GROUP BY [OPI]
在这种情况下,COUNT(*)
指的是该组中的记录数。
然后,您将获得一组(OPI, COUNT)
条目,您可以根据需要进行处理。如果使用适当的集合(如字典),这甚至可以使代码更清晰。
OPI
上的过滤器是可选的,如果只计算一小部分OPI值,则主要有用。