我有一个来自sql查询的记录集,我从中得到以下结果:
---------------
| Column_Color |
|---------------|
| Blue |
| Red |
| Magenta |
| Red |
| Grey |
如您所见,我有2条 RED 颜色的记录。
我想知道是否有任何可能的方法或方法来计算所有具有RED颜色的记录,来自Column_Color
列?
我试图避免使用SELECT COUNT
方法创建新查询,并且我想知道是否有任何可能的方法来计算现有记录集中的RED颜色。期望的结果是得到数字2.
我的真实记录集如下:我想计算名为dbo.Products.F_antislip_sub的字段
<%
Dim RS_proionta
Dim RS_proionta_cmd
Dim RS_proionta_numRows
Set RS_proionta_cmd = Server.CreateObject ("ADODB.Command")
RS_proionta_cmd.ActiveConnection = MM_sindesi_STRING
RS_proionta_cmd.CommandText=sql_q 'Here is my query...
RS_proionta_cmd.Prepared = true
Set RS_proionta = RS_proionta_cmd.Execute
RS_proionta_numRows = 0
%>
答案 0 :(得分:2)
将记录集的CursorLocation属性设置为adUseClient
后,您可以在记录之间移动并在本地应用过滤器。
Const adUseClient = 3
Dim RS_proionta
Dim RS_proionta_cmd
Dim RS_proionta_numRows
Set RS_proionta_cmd = CreateObject ("ADODB.Command")
RS_proionta_cmd.ActiveConnection = MM_sindesi_STRING
RS_proionta_cmd.CommandText=sql_q 'Here is my query...
RS_proionta_cmd.Prepared = true
Set RS_proionta = CreateObject ("ADODB.Recordset")
RS_proionta.CursorLocation = adUseClient
RS_proionta.Open RS_proionta_cmd
RS_proionta.Filter = "Column_Color = 'Red'" 'filter Reds
Dim redCount
redCount = RS_proionta.RecordCount 'redCount must equals to 2 now
Response.Write "There are " & redCount & " reds."
RS_proionta.Filter = "" 'remove filter
If Not RS_proionta.EOF Then
RS_proionta.MoveFirst ' move cursor back to beginning
End If
'now other jobs with RS_proionta can be done again from scratch
RS_proionta.Close
注意:此方法不应始终被视为最快的方法。如果您的记录集是一个庞大的记录集,其中包含大量数据的行和列,那么由于数据库服务器优化和服务器端游标,显式Select Count
查询可能比这更快。