2012-09-18 24 views
1

我是Stack Overflow和ASP的新手,但是這個網站已經幫我多次了!我對ASP和VBS非常不熟悉,但對PHP更加熟悉,所以如果有針對我的問題的PHP解決方案,那也可以。按日期排序,但類似的字段組

有一點背景 - 我的訪問數據庫有兩個表(與此查詢有關),其中一個名爲SignUpLog,另一個是NotesSignUpLog.FirstNoteAddr字段對應於另一個表中的Notes.NoteKey字段。

我已經成功地顯示了數據庫中的所有條目,但是我想要將特定患者的所有條目組合在一行中,同時仍按日期排序(最新的位於頂部)。

這裏是我的代碼:

Set DataConn = Server.CreateObject("ADODB.Connection") 
Set RS = Server.CreateObject("ADODB.RecordSet") 
DataConn.Open "DBQ=" & Server.Mappath("/path/to/mydb") & ";Driver={Microsoft Access Driver (*.mdb)};Uid=user;Pwd=pass;" 

Set rsDsp = DataConn.Execute("SELECT SignUpLog.PatientFileNumber, SignUpLog.ArrivalDateTime, Notes.Note, SignUpLog.Called, SignUpLog.DrName FROM SignUpLog, Notes WHERE (((Notes.NoteKey)=[SignUpLog].[FirstNoteAddr])) ORDER BY SignUpLog.ArrivalDateTime DESC;") 

If rsDsp.EOF Then 
    Response.Write "Sorry, no entries in the database!" 
Else 
%> 
<div align="center"><center> 
    <table BORDER="0" width="700"> 
     <tr> 
      <th width="105">Name</th> 
      <th width="105">Arrival Time</th> 
      <th width="105">Doctor</th> 
      <th width="105">Notes</th> 
     </tr> 
<% 
    While Not rsDsp.EOF 
    x = x + 1 
    If x = 1 Then 
     Response.Write "<TR><TD>" & rsDsp.Fields.Item("PatientFileNumber").Value & "</TD>" 
     Response.Write "<TD>" & rsDsp("ArrivalDateTime").Value & "</TD>" 
     Response.Write "<TD>" & rsDsp("DrName").Value & "</TD>" 
     Response.Write "<TD>" & rsDsp("Note").Value & "</TD></TR>" 
    Else 
     Response.Write "<TR><TD BGCOLOR=E4E4E4>" & rsDsp.Fields.Item("PatientFileNumber").Value & "</TD>" 
     Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("ArrivalDateTime").Value & "</TD>" 
     Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("DrName").Value & "</TD>" 
     Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("Note").Value & "</TD></TR>" 
     x = 0 
    End If 

    rsDsp.MoveNext 
    Wend 
    Response.Write "</TABLE>" 


    DataConn.Close 

    End If 
    %> 
</table> 
</center></div> 

這給我類似這樣的輸出:

Patient A | 9/18/2012 12:56:21 PM | Appt | Note1 
Patient A | 9/18/2012 12:56:21 PM | Appt | Note2 
Patient A | 9/18/2012 12:56:21 PM | Appt | Note3 
Patient B | 9/18/2012 1:56:21 PM | WalkIn | Note1 
Patient B | 9/18/2012 1:56:21 PM | WalkIn | Note2 

什麼,我想是這樣的:

Patient A | 9/18/2012 12:56:21 PM | Appt | Note1, Note2, Note3 
Patient B | 9/18/2012 1:56:21 PM | WalkIn | Note1, Note2 

我我試圖玩Group By並保持因爲我沒有試圖做任何數學的事情,這讓我很困惑。就像我說的,我是一個完整的ASP noob,我不是一個程序員。

回答

0

我相信那裏已經有針對您的問題的解決方案,請嘗試查看THIS的問題。你只需要定義(有點複雜)的功能,並用它作爲例子。

+0

這是有點過頭了。對於我想要做的事似乎有點複雜。也許你可以詳細說明一下,請記住我在這個大聲笑noobot –

0

這實際上是一個SQL查詢問題,與ASP無關。由於您使用的是MDB,因此您可能會發現使用MS Access建模查詢更容易,然後將生成的SQL語句粘貼回您的ASP代碼。

以下問題可以幫助:

SQL Group with Order by

+0

我不確定這是一個SQL答案或我正在尋找的ASP(表)答案。我在你的帖子中嘗試瞭解決方案,但不斷收到錯誤,我試圖訂購的列不是聚合函數的一部分。感謝您的快速回復! –

+0

在MS Access可視化查詢生成器界面中嘗試做你想做的事情,這使得它非常容易。 –

0

嚴重的HTML失敗。

無論如何,這裏有一個快速的解決方案,通過對結果集進行循環並顯示它的邏輯做一些mod。

<% 
Set DataConn = Server.CreateObject("ADODB.Connection") 
Set RS = Server.CreateObject("ADODB.RecordSet") 
DataConn.Open "DBQ=" & Server.Mappath("/path/to/mydb") & ";Driver={Microsoft Access Driver (*.mdb)};Uid=user;Pwd=pass;" 

Set rsDsp = DataConn.Execute("SELECT SignUpLog.PatientFileNumber, SignUpLog.ArrivalDateTime, Notes.Note, SignUpLog.Called, SignUpLog.DrName FROM SignUpLog, Notes WHERE (((Notes.NoteKey)=[SignUpLog].[FirstNoteAddr])) ORDER BY SignUpLog.ArrivalDateTime DESC;") 

If rsDsp.EOF Then 
    Response.Write "Sorry, no entries in the database!" 
Else 
%> 
<div align="center"> 
    <table BORDER="0" width="700"> 
     <tr> 
      <th width="105">Name</th> 
      <th width="105">Arrival Time</th> 
      <th width="105">Doctor</th> 
      <th width="105">Notes</th> 
     </tr> 
<% 
    Dim LastPatient; 
    Dim Notes = ""; 
    Dim x = 0; 
    While Not rsDsp.EOF 

    If LastPatient = rsDsp.Field.Item("PatientFileNumber").Value Then 
     Response.Write "<br />" & rsDsp("Note").Value 
    Else 
     If LastPatient <> NULL Then 
     Response.Write "</td></tr>" 
     If x Mod 2 = 0 Then 
     Response.Write "<tr><td>" & rsDsp.Fields.Item("PatientFileNumber").Value & "</td>" 
     Response.Write "<td>" & rsDsp("ArrivalDateTime").Value & "</td>" 
     Response.Write "<td>" & rsDsp("DrName").Value & "</td>" 
     Response.Write "<td>" & rsDsp("Note").Value 
     Else 
     Response.Write "<tr><td bgcolor=""E4E4E4"">" & rsDsp.Fields.Item("PatientFileNumber").Value & "</td>" 
     Response.Write "<td bgcolor=""E4E4E4"">" & rsDsp("ArrivalDateTime").Value & "</td>" 
     Response.Write "<td bgcolor=""E4E4E4"">" & rsDsp("DrName").Value & "</td" 
     Response.Write "<td bgcolor=""E4E4E4"">" & rsDsp("Note").Value 
     End If 
     x = x + 1 
    End If 

    LastPatient = rsDsp.Fields.Item("PatientFileNumber").Value 
    rsDsp.MoveNext 
    Wend 
    Response.Write "</td></tr>" 


    DataConn.Close 

End If 
%> 
</table> 
</div> 
+0

感謝所有幫助的人,我最終使用mdb-export將數據直接導入到mysql中,然後我可以使用PHP和mysql中內置的函數獲得我想要的結果。 group_concat獲勝! –