Jump to content

getting php to work with relationship tables


DonaldFaulknor

Recommended Posts

is it users table or friends table?  The friends are listed in a friends table, wasn't aware I should use the users table at all.

select s.statuses,
       s.whens,
       u.id,
       u.name
from Statuses s
join Users u on (s.users_id = u.id)
join Friends f on (u.id = f.friend.id)
where f.users_id = 123
order by s.whens;

 

There's some things I don't understand about this.  s and u, is that suppose to be the name of the table?  Statuses.statuses and Users.id

also... FROM?  Statuses s - is the lowercase s the name of the column statuses inside the table Statuses?

Are the (parenthesis) part of the statement or just a note?

As pointed out before, storing user information within a friend relationship table is a bad idea. You said there was a separate table for the user data - I guessed a couple of the column names in my SQL. What's the point in showing the status if you can't say anything about who posted it? Joining the user data would be something you would need to do at some point.

 

s, u and f are just aliases:

 

[...]

from Statuses s

join Users u on (s.users_id = u.id)

join Friends f on (u.id = f.friend.id)

[...]

 

You could also declare them with an optional "as" if you find it easier:

 

[...] from Statuses as s [...]

 

The parentheses are also optional - personally I just prefer them.

 

collectively, I'm going to say that you should be studying more about Mysql and PHP before attempting to make the next Facebook like 100000000 other programmers. Study hard, learn, and then come back and attempt to make this kind of website the correct way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.