So i have a DB with a table in called 'friends' and in the table i have userID and FriendID, thus finding all the friends you would look up userID and display all of them..
If i have 5 friends and their names are Kyle, Joe, Bob, Dillion, and Josh.
When i go to look up my friends i want to know how to sort all those names alphabetically
so the table
___friend_____
--userID-- --friendID--
________________
_____________users______________
--ID -- --name-- --username-- --password--
_______________________________
a foreach within a foreach would work but im not sure how to save all data to an array then sort that array by name then echo, but thats not a very good way of doing it.. seems fairly slow..
5 replies to this topic
#1
Posted 09 August 2011 - 12:19 PM
|
|
|
#2
Posted 09 August 2011 - 12:21 PM
It should look like this :
SELECT name FROM friend,users WHERE friendID = ID ORDER BY name ASC;
#3
Posted 09 August 2011 - 12:22 PM
Select * from friend inner join users on friend.friendID = users.ID where friend.userID='me' order by users.name
#4
Posted 09 August 2011 - 12:25 PM
If you're using MySQL, you could do something like this:
And you can insert a 'WHERE' clause, too, if you need that.
SELECT * FROM `users` ORDER BY `name` ASCYou can also use 'DESC' instead of 'ASC', to order them backwards (first Joe, then Bob).
And you can insert a 'WHERE' clause, too, if you need that.
#5
Posted 09 August 2011 - 12:37 PM
WingedPanther said:
Select * from friend inner join users on friend.friendID = users.ID where friend.userID='me' order by users.name
Thank you for understanding what i was saying.. Its that easy ha! :D
#6
Posted 10 August 2011 - 04:38 AM
Joins are one of those things that take a while for most programmers to get the hang of. Actually, SQL is that way as well. We get so used to doing EVERYTHING using arrays, and other low-level features, that it never occurs to us that we can use the tool to do the heavy lifting for us.
With that said, I've done a few things in SQL where I tried to get it to do the heavy lifting, and I found out I was better off doing it the old-fashioned way.
With that said, I've done a few things in SQL where I tried to get it to do the heavy lifting, and I found out I was better off doing it the old-fashioned way.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









