标题:ASP+SQL问题,请各位帮助
只看楼主
yn_海浪
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-6-26
 问题点数:0 回复次数:2 
ASP+SQL问题,请各位帮助
在ASP+SQL中,用SQL语句查询了T1数据表,查询结果如下:
 stationID         obsdatatime           TT        Tmax
   5611      2017-06-12 08:00:00.000    25.0       30.0
   5611      2017-06-12 07:00:00.000    23.0       28.0
   5611      2017-06-12 06:00:00.000    20.0       23.0
   5612      2017-06-12 08:00:00.000    28.0       32.0
   5612      2017-06-12 07:00:00.000    25.0       28.0
   5612      2017-06-12 06:00:00.000    30.0       32.0
   5613      2017-06-12 08:00:00.000    24.0       26.0
   5613      2017-06-12 07:00:00.000    23.0       25.0
   5613      2017-06-12 06:00:00.000    25.0       32.0   
   .....
   .....
我要让查询结果显示在页面是如下:
stationID   T1(2017-06-12 08:00:00.000)   TT2(2017-06-12 07:00:00.000)    TT3(2017-06-12 06:00:00.000) ...    Tmax1(2017-06-12 08:00:00.000)   Tmax2(2017-06-12 07:00:00.000)  ...
  5611          25.0                             23.0                             20.0                              30.0                             28.0
  5612          28.0                             25.0                             30.0                              32.0                             28.0
  5613          24.0                             23.0                             25.0                              26.0                             25.0
  .....
这个结果怎么实现,请各位帮忙,谢谢!
搜索更多相关主题的帖子: 数据表 
2017-06-26 11:06
yn_海浪
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-6-26
得分:0 
向各位高手真心请教。
2017-06-29 14:49
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
得分:0 
非常简单

程序代码:
Private Sub Form_Load()
Dim db As New ADODB.Connection, RS As New ADODB.Recordset
Dim SST As Integer, SID() As Long, TT1() As Single, TT2() As Single, TT3() As Single, Tmax1() As Single, Tmax2() As Single, Tmax3() As Single, K As Integer    '声明变量与数组变量
db.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "\T1数据库.accdb;Jet OLEDB:Database Password=;"
db.Open                    '连接数据库
    RS.Open "Select distinct stationID From T1数据表", db, 3, 2   '得到 stationID 字段不重复记录,distinct 是关键字
        SST = RS.RecordCount
        ReDim SID(1 To SST), TT1(1 To SST), TT2(1 To SST), TT3(1 To SST), Tmax1(1 To SST), Tmax2(1 To SST), Tmax3(1 To SST)   '声明动态数组变量的下标
    Do While Not RS.EOF
        K = K + 1
        SID(K) = RS!stationID    '取得不重复的 stationID 记录存储在 SID 数组
    RS.MoveNext
    Loop
    RS.Close
    Set RS = Nothing
For I = 1 To SST    '循环,得到需要的数据
    RS.Open "Select * From T1数据表 Where stationID=" & SID(I) & "", db, 3, 2
    Do While Not RS.EOF
        If RS!obsdatatime = "2017-06-12 08:00:00.000" Then TT1(I) = RS!TT: Tmax1(I) = RS!Tmax  '如果 obsdatatime 的记录是  "2017-06-12 08:00:00.000",分别存储在 TT1 与 Tmax1 的数组中
        If RS!obsdatatime = "2017-06-12 07:00:00.000" Then TT2(I) = RS!TT: Tmax2(I) = RS!Tmax  '如果 obsdatatime 的记录是  "2017-06-12 07:00:00.000",分别存储在 TT2 与 Tmax2 的数组中
        If RS!obsdatatime = "2017-06-12 06:00:00.000" Then TT3(I) = RS!TT: Tmax3(I) = RS!Tmax  '如果 obsdatatime 的记录是  "2017-06-12 06:00:00.000",分别存储在 TT3 与 Tmax3 的数组中
    RS.MoveNext
    Loop
    RS.Close
    Set RS = Nothing
Next I
db.Close   '关闭数据库
Set db = Nothing
MSHFlexGrid1.Clear
MSHFlexGrid1.Visible = True
With MSHFlexGrid1
    .Cols = 7
    .Rows = 1 + SST
    .TextMatrix(0, 0) = "stationID"
    .TextMatrix(0, 1) = "TT1"
    .TextMatrix(0, 2) = "TT2"
    .TextMatrix(0, 3) = "TT3"
    .TextMatrix(0, 4) = "Tmax1"
    .TextMatrix(0, 5) = "Tmax2"
    .TextMatrix(0, 6) = "Tmax3"
    .ColWidth(0) = 1300
    .ColWidth(1) = 800
    .ColWidth(2) = 800
    .ColWidth(3) = 800
    .ColWidth(4) = 1000
    .ColWidth(5) = 1000
    .ColWidth(6) = 1000
    For I = 1 To SST   '在 MSHFlexGrid1 控件显示记录
    .TextMatrix(I, 0) = SID(I)
    .TextMatrix(I, 1) = Format(TT1(I), "0.0")
    .TextMatrix(I, 2) = Format(TT2(I), "0.0")
    .TextMatrix(I, 3) = Format(TT3(I), "0.0")
    .TextMatrix(I, 4) = Format(Tmax1(I), "0.0")
    .TextMatrix(I, 5) = Format(Tmax2(I), "0.0")
    .TextMatrix(I, 6) = Format(Tmax3(I), "0.0")
    Next I
End With
End Sub

是不是特简单?这个是使用Access数据库与VB实现的,不是ASP+SQL数据库,稍稍修改一下就可以了。

[此贴子已经被作者于2017-7-9 11:30编辑过]


请不要选我!!!
2017-07-09 11:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-478937-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.301783 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved