开发者

Run multiple queries then display them based on weighted importance like Facebook Wall

I am building a social network site for a client and one of things that has been troubling me is how I am going to display status updates, new blog posts, profile comments etc. on their "wall". The problem is not querying the database and displaying the information... it is displaying all the information within 1 query. For example, lets say I have the following tables:

Members, 
MemberComment, 
Statuses, 
Articles

I know I can display member statuses in one query like this:

SELECT * FROM status WHERE who = '$profileid' ORDER BY ID DESC

And then I can display comments like this:

SELECT * FROM member_comments WHERE which = '$prof开发者_运维问答ileid' ORDER BY ID DESC

I am using while loops to display each section of data, one while loop for the comments, one for statuses, and so on.

How can I display this information in a way where it is sorted by date posted, or level of importance? I don't know how to condense all the queries into one, and then on top of that, be able to display it the right way in a while loop.


One example for member status

select member.*, status.* from member m
  inner join status s on m.member_id = s.who 
order by s.ID desc limit 1


If you are using MySQL, I would use the GROUP_CONCAT function to get all your children and put them into one row for the parent in a new column. Then you can parse that field on your server side to show each result individually.

See: http://dev.mysql.com/doc/refman/4.1/en/group-by-functions.html#function_group-concat


Assuming you have some kind of similarity between the various tables, and some kind of idea about how to deal with relevance, you could use the UNION operator. It's like magic!

Sample query:

select title, 
    date, 
    100 as importance
from memberComment
where originalPostMemberID = $currentUserID
union
select title, 
  date, 
  1000 as importance
from statuses
where (some way of working out visibility of the status request)
union 
select title, 
  date, 
  1010 as importance
from articles
where (some way of working out visibility of the status request)
order by date desc, importance


This is what I ended up going with, and it works fine for now. It returns both comments and statuses and orders them by date posted...

    SELECT
    member_comments.what AS commentWHAT,
    member_comments.who AS commentWHO,
    member_comments.when AS commentWHEN,
    members.name AS commentNAME
    FROM member_comments, members
    WHERE member_comments.who = members.id AND 
    member_comments.which = '$id'
    UNION
    SELECT
    status.what AS statusWHAT,
    status.who AS statusWHO,
    status.when AS statusWHEN,
    members.name AS statusNAME
    FROM status, members
    WHERE status.who = '$id' AND members.id = '$id'
    ORDER BY commentWHEN DESC

Then I echo out the data as follows:

            while ($row = mysql_fetch_array($result)) {
            $message = $row['commentWHAT'];
            $commentWHO = $row['commentWHO'];
            $commentNAME = $row['commentNAME'];
            $statusWHO = $row['statusWHO'];

            echo '
            <div class="fairway_thing">
                <img src="images/avatar-60.jpg">
                <p><strong><a href=profile.php?uid=' . $commentWHO . '>' . $commentNAME .'</a></strong> ' . $message . '</p>
                <p><a href="#">Like</a> <a href="#">Tweet</a> <a href="#">Share</a></p>
            </div>
            <div class="clear"></div>
            ';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜