webForumDet fria alternativet
Logga in / Bli medlem

Distinct sökfunktion

Databaser & SQL

9 svar · 125 visningar · startad av erka

Medlem sedan dec. 19996 522 inlägg
Frågan#1

Kollat igenom men inte hittat hur man löser det på ett smidigt sätt, jag vill att den ska bara skriva ut en topic samt länken om han hittar flera i samma topicsubject, repliemessage, topicmessage och sen hoppa till nästan, någon form av distinct select satts alltså..

Nu ser koden ut

strSQL = "SELECT dbo.Forum.ForumID, dbo.Replies.ReplieId, dbo.Replies.TopicID, dbo.Replies.ReplieBy, dbo.Replies.ReplieMessage, dbo.Replies.ReplieDate, "_
&"dbo.Topics.TopicID, dbo.Topics.ForumID, dbo.Topics.TopicBy, dbo.Topics.TopicSubject, dbo.Topics.TopicMessage, dbo.Topics.TopicDate "_
&"FROM dbo.Forum INNER JOIN dbo.Topics ON dbo.Forum.ForumID = dbo.Topics.ForumID INNER JOIN dbo.Replies ON dbo.Topics.TopicID = dbo.Replies.TopicID "_
&"WHERE dbo.Topics.ForumID = "& strForumId &" AND dbo.Forum.ForumID = "& strForumId &" "_
&"AND dbo.Replies.ReplieMessage LIKE '%" & strSearch & "%' OR dbo.Topics.TopicSubject LIKE '%" & strSearch & "%' "_
&"OR dbo.Topics.TopicMessage  LIKE '%" & strSearch & "%'"

Men nu får jag ju fram flera som ni kanske ser av koden. Någon som har en bra lösning av mitt problem ?

Medlem sedan dec. 200012 464 inlägg
#2

Jag säger bara ett ord:

använd union

Medlem sedan dec. 19996 522 inlägg
#3

hmm ser ju det nu :) vad tror du om den här

strSQL ="SELECT DISTINCT dbo.Forum.ForumID, dbo.Forum.ForumName, dbo.Topics.TopicID, dbo.Topics.ForumID, dbo.Topics.TopicSubject, "_
&"dbo.Topics.TopicMessage, dbo.Topics.TopicDate FROM dbo.Forum INNER JOIN dbo.Topics ON dbo.Forum.ForumID = dbo.Topics.ForumID "_
&"WHERE dbo.Forum.ForumID = "&strForumId&" AND (dbo.Topics.TopicSubject LIKE '%" & strSearch & "%' OR dbo.Topics.TopicMessage LIKE '%" & strSearch & "%' ) "_
&"UNION "_
&"SELECT DISTINCT dbo.Forum.ForumID, dbo.Forum.ForumName, dbo.Replies.ReplieId, dbo.Replies.TopicID, dbo.Replies.ReplieBy, dbo.Replies.ReplieMessage, "_
&"dbo.Replies.ReplieDate FROM dbo.Forum CROSS JOIN dbo.Replies "_
&"WHERE dbo.Forum.ForumID = "& strForumId &" AND dbo.Replies.ReplieMessage LIKE '%" & strSearch & "%'"

Funkar verkar den göra men är den ful :e

tack larsg

Medlem sedan dec. 200012 464 inlägg
#4

Om den fungerar så är det väl okej.

Om du använder union så behöver du inte ha distinct med då union innebär att man tar bort alla duplikat från resultatet.

Medlem sedan dec. 19996 522 inlägg
#5

stötte på problem nu, den plockar inte ut rätt. Det är ju ett forum där jag har 3 tabeller

Forum (där jag behöver ha ut)
ForumId

Replies (där jag behöver ha ut)
TopicID
ReplieMessage
ReplieDate

Topics (där jag behöver ha ut)
TopicId
ForumId
TopicSubject
TopicMessage
TopicDate

Problemet är att det inte fungerar med en union eftersom jag måste ha lika många fält och av samma typ i en union. Vad ska man göra istället ?

Medlem sedan dec. 200012 464 inlägg
#6

Du kan ju alltid lägga till konstanter i ena delen

  select a,b,c from t1
  union 
  select a,b,'x' from t2

pss kan du alltid få till att det är lika många och av samma typ.

Medlem sedan dec. 19996 522 inlägg
#7

Problemet blir att jag skriver ut det så här

<%
strSQL="SELECT dbo.Forum.ForumID, dbo.Topics.TopicID, dbo.Topics.ForumID, dbo.Topics.TopicSubject, dbo.Topics.TopicMessage, "_
&"dbo.Topics.TopicDate FROM dbo.Forum INNER JOIN dbo.Topics ON dbo.Forum.ForumID = dbo.Topics.ForumID "_
&"WHERE  dbo.Forum.ForumID = "&strForumId&" AND (dbo.Topics.TopicSubject LIKE '%" & strSearch & "%' OR dbo.Topics.TopicMessage LIKE '%" & strSearch & "%' ) "_
&"UNION "_
&"SELECT dbo.Forum.ForumID, dbo.Replies.ReplieId, dbo.Replies.TopicID, dbo.Replies.ReplieBy, dbo.Replies.ReplieMessage, dbo.Replies.ReplieDate "_
&"FROM dbo.Forum CROSS JOIN dbo.Replies "_
&"WHERE dbo.Forum.ForumID = "& strForumId &" AND dbo.Replies.ReplieMessage LIKE '%" & strSearch & "%'"

Set RS = Connect.Execute(strSQL)
If Rs.Eof Then
Response.Write"Hittade NADA"
Rs.Close
Set RS = Nothing
Else
Dim ArrRs, i
ArrRs = Connect.Execute(strSQL).getrows()
for i = 0 to ubound(ArrRs,2)
%>
        <br><a href="ShowTopic.asp?ForumId=<%=ArrRs(0,i)%>&TopicId=<%=ArrRs(2,i)%>"><%=ArrRs(4,i)%></a> <%=Left(ArrRs(5,i),16)%> : <%=ArrRs(5,i)%>
        <br>
		<%
Next
End If
Connect.Close
Set Connect = Nothing
		%>

Och länkarna blir rätt på vissa men vissa får helt fel TopicId. Vad fean beror det på tro ?

Medlem sedan dec. 19996 522 inlägg
#8

Glöm det, hade ju skrivit dem på fel plats i de båda sattserna :) återkommer om jag stöter på mer probs.danke! trevlig helg föresten, ska jobba till 9 ikväll, suck :e

Medlem sedan dec. 200012 464 inlägg
#9

Dags att säga det nu. Jag hade redan lagt ner 1 minut på att hitta det.

Varför har du inget villkor på replies i den andra union-grenen?

(Sats stavas med 1 t)

Medlem sedan dec. 19996 522 inlägg
#10

Det är bara ett litet problem nu, jag har pulat å fulat med unionen.

nu ser den ut

strSQL="SELECT DISTINCT dbo.Forum.ForumID, dbo.Topics.TopicID, dbo.Topics.ForumID, dbo.Topics.TopicSubject, dbo.Topics.TopicMessage, "_
&"dbo.Topics.TopicDate FROM dbo.Forum INNER JOIN dbo.Topics ON dbo.Forum.ForumID = dbo.Topics.ForumID "_
&"WHERE  dbo.Forum.ForumID = "&strForumId&" AND (dbo.Topics.TopicSubject LIKE '%" & strSearch & "%' OR dbo.Topics.TopicMessage LIKE '%" & strSearch & "%' ) "_
&"UNION "_
&"SELECT DISTINCT dbo.Forum.ForumID, dbo.Replies.TopicID, dbo.Topics.ForumID, dbo.Topics.TopicSubject, dbo.Replies.ReplieMessage, dbo.Topics.TopicDate "_
&"FROM dbo.Forum INNER JOIN dbo.Topics ON dbo.Forum.ForumID = dbo.Topics.ForumID INNER JOIN dbo.Replies ON dbo.Topics.TopicID = dbo.Replies.TopicID "_
&"WHERE dbo.Forum.ForumID = "& strForumId &" AND dbo.Replies.ReplieMessage LIKE '%" & strSearch & "%'"

Har märkt att den skriver ut 2 av sakerna ibland, jag tror det har att göra med att jag har stält något fält fel men ser inte hur jag annars ska lösa det. Titta på det om du har tid och ork ;)

272 ms totalt · 4 externa anrop · v20260731065814-full.5386d3bf
132 ms — deklarationer (db)
0 ms — hämta statistik (cache)
136 ms — hämta tråd, inlägg och bilagor (db)
133 ms — ändringar (db)