I have this table called user_friends:
accid | user1 | user2
1 mark_12 john_24
2 john_24 jim_01
3 mark_12 jim_01
4 nico_7 mark_12
And this table called users_students:
studentuniqueid | firstname | lastname | age
mark_12 Mark Anthony Sierra 20
john_24 Johhny Powers 21
jim_01 James Sanders 21
nico_7 Nicolai Renade 19
janice_53 Janice Allaine 24
peter_41 Peter Allan 21
Now, here's what I wanted to do:
1.Select all entry/data in the user_friends table that has mark_12 (Mark's ID). Mark's ID could either be on user1 or user2 right? So I can just use SELECT * FROM user_friends WHERE user1 = 'mark_12' OR user2 = 'mark_12' and echo its accid, it will then display:
1, 3, 4 since mark_12 appears on those entries on the table.
2.Now I want to select and link the user_friends and users_student so that I could alphabetically arrange Mark's friends based on the user_friends table ORDERING them based on the lastname on the users_student table. I know this is possible, I just don't know how to do it yet.
I tried:
$user_id = $_SESSION['id'];
$sql = "SELECT user_friends.accid,users_student.studentuniqueid FROM user_friends INNER JOIN IN (user_friends.user1,user_friends.user2)=users_student.studentuniqueid WHERE user1 = '$user_id' OR user2 = '$user_id' ORDER BY users_student.lastname ASC ";
And also:
$sql = "SELECT user_friends.accid,users_student.studentuniqueid FROM user_friends INNER JOIN user_friends.user1,user_friends.user2=users_student.studentuniqueid WHERE user1 = '$user_id' OR user2 = '$user_id' ORDER BY users_student.lastname ASC ";
But somehow didn't work. I want to achieve something like:
Mark's Friends:
Johnny Powers
Nicolai Renade
James Sanders