tags:

views:

85

answers:

3

Hi there!

I have a problem that I can't figure out myself. I've tried using LEFT JOIN etc but nothing seems to work. I'm using MySQL so you know.

I'm building a little blogportal for me and my friends and all users have their own blog.

database:

users: id, username, password, etc

blog: id, title, text, user_id, etc

relations follower_id, following_id

I query my own blogposts like this:

SELECT * FROM microblog WHERE user_id = {$user_id} ORDER BY posted DESC

and i list my friends like this:

SELECT * FROM users, relations WHERE relations.follower_id = {$user_id} AND relations.following_id = users.id

That was the easy part. BUT.

I rather JOIN the tables somehow because I also want to list my friends blogposts inside my loop. But I don't just want the post to show, I also want some info about the user that posted that one so then I must get some info from the users table as well. And that's what bothers me! I can't figure it out.

In short: I want to list my own blog posts and all the users I'm friend with within my own loop. And I also want to display username and email beside the posts.

Hope you understand what I mean.

/Tobias

Sweden

A: 

Try this?

SELECT m.* 
FROM microblog m
INNER JOIN users u ON m.user_id = u.user_id
LEFT JOIN relations r ON r.following_id = m.user_id
WHERE m.user_id = {$user_id}
OR (r.follower_id = {$user_id} AND NOT IsNull(r.follower_id))
ORDER BY posted DESC
Spencer Ruport
This helps me list my own posts and my friends but I also want to list who wrote them. So I need to get username from users table also
Tobias
There. Does that work?
Spencer Ruport
A: 

How about?

select 
    u.username, 
    u.email, 
    m.title, 
    m.text 
       -- ... etc
from microblog m
     inner join user u on m.user_id = u.id
where  m.user_id = {$user_id} 
    or m.user_id in (select 
                         following_id 
                     from relations r 
                     where follower_id = {$user_id}
                    );
Stobor
That worked like a charm when I changed: where following_id = {$user_id}towhere follower_id = {$user_id}Now its working and showing everything in phpmyadmin but when I try to echo it out then only the data in "microblog" table works. if I for example use echo $mblog->username; in a foreach loop it doesn't work, I get:Undefined property: mBlog::$username in.......but it should be there?
Tobias
@Tobias: oops, yeah, I meant follower there... :) Are you saying that $mblog->title works but $mblog->username fails? Can you post the code you're using there?
Stobor
I'm using 2 classes, one for users and one for microblog so I believe I need to pass the username into my microblog class somehow. I tried to echo it out the no oop way and then it worked like a charm.
Tobias
Hmmm... It depends on how your classes are structured. I don't know how you're constructing your objects... But one thought might be: ignore the user table in your initial query, and have a bloguser member in your mBlog class which returns "new User($this->user_id)". Then you can call $mblog->bloguser->username instead.
Stobor
(Beware that this does lead to the "SELECT N+1" scenario, which might have a significant negative impact on your performance. For that reason, I usually choose the non-OOP method for such problems where possible.
Stobor
hmm I guess that I'm going back to the traditional way then. I don't like the idea of mixing OOP and traditional. But thanks for all your answers. If you come up with something then, tobbesanda [at] gmail, me.. Thanks again!
Tobias
A: 

From my perspective I would pull the user data separately, store them into an array and access them when needed. This should be better for performance and would definitely be simpler.

select * from blog b, relations r 
where b.user = $user_id or ( b.user = r.follower_id and r.following_id = $user_id )
order by posted desc;

Not sure if I reversed follower/following.

therealsix