[SQL] JOIN 3 Tabellen

Pagina: 1
Acties:

  • Mischa_NL
  • Registratie: Mei 2004
  • Laatst online: 01-02-2023
In ASP probeer ik een forum te maken. Maar nu liep ik tegen dit probleem aan:
Op het moment dat ik de topics op een pagina van een forum laat zien, geeft hij de lastposter van het topic en de volgende 2 topics weer.

De tabellen: (gedeeltes uiteraard)

tblForum:
code:
1
ID  Forum_ID    Topics_Per_Page


tblPosts:
code:
1
Post_ID Topic_ID    Auther  Message Date


tbltopics:
code:
1
Topic_ID    Topic_Auther    Last_Entry


tblUsers:
code:
1
User_ID UserName


De query's:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if Request.QueryString("pagina") = "" Then
huidigePagina = 1
Else
huidigePagina = CInt(Request.QueryString("pagina"))
End if

Dim objRec2
Set objRec2 = Server.CreateObject("ADODB.Recordset")

SQL = "SELECT UserName, User_ID, Topics_Per_Page FROM tblTopics, tblPosts, tblUsers, tblForum WHERE (Post_ID = Last_Entry) AND (User_ID = Auther) ORDER BY Date DESC"
Set objRec2 = objCon.execute(SQL)

aantalRecords = objRec2.fields("Topics_Per_Page")

Dim objRec
Set objRec = Server.CreateObject("ADODB.Recordset")

SQL = "SELECT tblTopics.Topic_ID, Topic_Subject, Topic_Auther, UserName, Views, Date, Last_Entry FROM tblTopics, tblUsers, tblPosts "
SQL = SQL & "WHERE User_ID = Topic_Auther AND Post_ID = Last_Entry AND Forum_ID=" & request.QueryString("f")
SQL = SQL & " ORDER BY Date DESC"

objRec.Open SQL, strCon, adOpenStatic, adLockReadOnly, adCmdText
if objRec.recordcount > 0 then
objRec.PageSize = aantalRecords
objRec.CacheSize = aantalRecords
aantalPaginas = objRec.PageCount

if huidigePagina > aantalPaginas Then
huidigePagina = aantalPaginas
End if

objRec.AbsolutePage = huidigePagina
recordsGetoond = 0 %>
  <table border="0" cellpadding="0" cellspacing="0" align="left" width="100%"><% Do While recordsGetoond < aantalRecords And Not objRec.EOF %><%
  SQL = "SELECT COUNT(*) AS cposts FROM tblposts WHERE Topic_ID = " & objRec.Fields("Topic_Id")
  Set objRec3 = objCon.execute(SQL) %>

'Hier worden de gegevens neergezet (Topic, Topicstarter, posts, views, lastpost)

<% recordsGetoond = recordsGetoond + 1
objRec2.movenext
objRec.movenext
loop %>
  </table> 'tabel afsluiten


Afbeeldingslocatie: http://website.pcextreme.nl/imageuploader/uploads/tweakers.net.jpg

Zoals je ziet is er op de plek waar ik de pijl zet GEEN replie (ookal staat er 1, dat is de topic post) maar TOCH verschilt de de topicstarter met de last poster :+

ik hoop dat jullie me kunnen helpen om de lastposter goed te krijgen want ik zit er nu al een uur mee te klooien :)

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 14:53

NMe

Quia Ego Sic Dico.

Haal in een subquery de naam op van de gebruiker die het laatste bericht plaatste:
code:
1
2
3
4
5
6
7
SELECT U.UserName FROM tblUsers U, tblPosts P, tblTopics T
WHERE U.User_ID = P.auther
AND P.Date = (
  SELECT MAX(Date)
  FROM tblPosts
  WHERE topic_id = P.Topic_ID
) AND T.Topic_ID = P.Topic_ID

Of iets dergelijks.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.