forum: sql sort in meerdere tabellen

Status
Niet open voor verdere reacties.

That Guy

Meubilair
Lid geworden
28 nov 2006
Berichten
5.010
Yo,

ik heb twee tabellen, 'posts' en 'threads'.

posts
id (int)
time (int) (UNIX stamp)
thread (int) (ID van thread in tabel 'threads')


threads
id (int)
sticky (int) (0 of 1)
name (text) (naam thread)
topic (int) (gaat t ff niet om)



ik probeer deze query:
PHP:
$id = 42; //gaat t even niet om

$q = mysql_query("

   SELECT DISTINCT threads.id, threads.name
   FROM threads, posts
   WHERE threads.topic='". $id ."'
   ORDER BY threads.sticky DESC, posts.time DESC

");

maar dit wil helaas niet werken (kan zoiets zowiezo?). Iemand een idee hoe ik de threads zo kan orderen, dat eerst op sticky threads wordt geordend, en daarna op de tijd?


:thumb:
 
Laatst bewerkt:
SELECT DISTINCT threads.id, threads.name
FROM threads
JOIN posts
ON t.id = posts.thread
WHERE threads.topic='". $id ."'
ORDER BY threads.sticky DESC, posts.time DESC

EDIT:
Die DISTINCT is wel raar, waarom doe je dat? Topics staan er toch niet dubbel in ofzo?
 
Laatst bewerkt:
Hey,

bedankt voor je reactie, dat was precies wat ik zocht.

Nu komt alles er goed in, toen werd een thread voor elke post geshowed:

sqlferr1.png


het 'distinct' haalde dit dus weg.


opgelost! :thumb:
 
Als je LEFT JOIN gebruikt heb je DISTINCT niet nodig denk ik, moet je even mee stoeien.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan