views:

109

answers:

3

Hello, i have this 3 tables

forums_forum

+-----+--------+-------------+-------+-----+
| fid | name   | description | index | cid |
+-----+--------+-------------+-------+-----+
|  36 | gdghdf | hjghj       |    54 |   5 |
|  45 | yutuy  | iuyi        |    99 |   6 |
+-----+--------+-------------+-------+-----+

forums_threads

+----+-----+-----+-------+-------+------+-----------+------+
| id | tid | fid | moved | mfrom | view | important | lock |
+----+-----+-----+-------+-------+------+-----------+------+
|  1 |   4 |  36 |     0 | NULL  |    0 |         0 |    0 |
|  2 |  12 |  36 |     0 | NULL  |    7 |         0 |    0 |
|  3 |   9 |  15 |     0 | NULL  |    0 |         0 |    0 |
+----+-----+-----+-------+-------+------+-----------+------+

forums_posts

+----+-------+--------+--------+---------------------+--------+--------+-----+
| id | title | detail | author | date                | edited | editby | tid |
+----+-------+--------+--------+---------------------+--------+--------+-----+
|  1 | asfsd | sdfsd  |      1 | 2010-07-01 21:31:29 |      0 | NULL   |   4 |
+----+-------+--------+--------+---------------------+--------+--------+-----+

I'm trying to create query which return result -> for every unique 'fid', one row from 'forums_posts' (ORDER BY 'date').

forums_forum.fid = forums_threads.fid forums_threads.tid = forums_posts.tid

Thanks

+1  A: 

Well i suggest some JOINs for you.

SELECT C.date, C.title, A.name
FROM forums_forum A 
   JOIN forums_threads B ON A.fid=B.fid 
   JOIN forums_posts C ON B.tid=C.tid
ORDER BY C.date DESC LIMIT 1

or .. not tested:

SELECT MAX(c.date), C.date, C.title, A.name
FROM forums_forum A 
   JOIN forums_threads B ON A.fid=B.fid 
   JOIN forums_posts C ON B.tid=C.tid
LIMIT 1

;-)

p.s. you might get troubles by naming a column "date" as well as "index" and "view", better use other names.

p.p.s. are there fids (i assume this is your primary key and should be unique) whiche occure more then once?

helle
*forums_forum, plus: I think the desired result should come from the forums_posts table plus you forgot the ordering.
Dave
thanks dave ... one moment ;-)
helle
i've tried this query but he return me a lot of result for one 'tid'. I want one result from unique 'tid' and to be the last, ordered by 'date'
Blood
I guess you can't use all joins then.
casablanca
@Blood: In your question you wrote that you want a result for every unique fid, now you say for every unique tid. Which is it?
Mark Byers
one row for every selected forum! If i have 5 forums i want 5 result which are the last posts
Blood
The Forums are 'fid' and the Topics are 'tid', but it have many topics in one forum and only one post which is latest from any topics which is in selected forum.
Blood
For Example: I have Forums - fid - 1,3,5.I have Topics - fid - 1 tid: [2,5,7]I have posts - tid: 2 [4,6,7], tid: 5 [9,12,3], tid: 7 [10,11,8]i want the latest post from this 9 posts
Blood
Table 'forums_forum' - `fid` is unique AITable 'forums_threads' - `tid` is unique, `fid` can be repeatedTable 'forums_posts' - `tid` can be repeated, `id` is unique AI
Blood
@helle: The problem here is that as long as the `LIMIT 1` is part of the outer query, you'll get only 1 result in all, while the OP wants 1 result per forum.
casablanca
@helle it's hard to code to an unclear, changing specification
Dave
+2  A: 

This is the venerable greatest-n-per-group problem that comes up frequently on Stack Overflow. Here's a solution given your tables:

SELECT p.* FROM forums_posts p JOIN forums_threads t ON p.tid = t.tid
WHERE NOT EXISTS (
    SELECT * FROM forums_posts p2 JOIN forums_threads t2 ON p2.tid = t2.tid
    WHERE t.fid = t2.fid AND p.date < p2.date
);
Bill Karwin
this is working excellent! Thanks!!!
Blood
A: 

Alternative to a JOIN, you can add a column to your forums_forums table with a name like last_updated. On every posting to a thread, simply run an additional UPDATE like.

UPDATE forums SET last_updated = NOW()

Then, to get the forums by the order of forums, your SELECT becomes much simpler, and performant.

SELECT * FROM forums_forum ORDER BY last_updated DESC

Chris Henry