daniel, , .
TABLE User (id, username, location )
TABLE Interests(id, hobby )
TABLE UserInterest(userId, interestId, level )
users : id.
- , - .
Interest ,
(ehm , , )
, users interests .
, ?
... user intrests intrests people.
, :
TABLE users (id, username, location, intrest1, intrest2, intrest3)
, , :
bad
SELECT * FROM user
INNER JOIN interests ON (user.intrest1 = interests.id) or
(user.intrest2 = interests.id) or
(user.intrest3 = interests.id);
, .
, .
" "
- .
" " " ".
A: 1 userinterest user
B: 1 userinterest interests
SELECT * FROM user
INNER JOIN userInterest ON (user.id = userInterest.userID) //many-to-1
INNER JOIN interest ON (interest.id = userInterest.InterestID); //many-to-1
?
, php - . , SQL , :
, Moe.
, . .
, user "Moe", user, , not "Moe".
, .
userinterest, , user interests.
SELECT i_Moe.hobby FROM interests AS i_Moe
INNER JOIN userInterests as ui2 ON (ui2.InterestID = i_Moe.id)
INNER JOIN user AS u_Moe ON (u_Moe.id = ui2.UserID)
WHERE u_Moe.username = 'Moe';
Moe.
SELECT u_Others.username FROM interests AS i_Others
INNER JOIN userinterests AS ui1 ON (ui1.interestID = i_Others.id)
INNER JOIN user AS u_Others ON (ui1.user_id = u_Others.id)
INNER JOIN Interests AS i_Moe ON (i_Moe.Hobby = i_Others.hobby)
INNER JOIN userInterests as ui2 ON (ui2.InterestID = i_Moe.id)
INNER JOIN user AS u_Moe ON (u_Moe.id = ui2.UserID)
WHERE u_Moe.username = 'Moe'
AND u_Others.username <> 'Moe';
INNER JOIN , , .
, .
, Moe, U_others.
Moe, U_Moe.
Moe , i_Moe
, i_Others
i_Others u_Moe
U_others, , Moe's
, .