Jump to content

Friends Sorted Alpha

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Whitey

Whitey

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
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..

#2
haltox

haltox

    Newbie

  • Members
  • PipPip
  • 29 posts
It should look like this :
SELECT name 

FROM friend,users

WHERE friendID = ID

ORDER BY name ASC;


#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Select * from friend inner join users on friend.friendID = users.ID where friend.userID='me' order by users.name
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
If you're using MySQL, you could do something like this:
SELECT * FROM `users` ORDER BY `name` ASC
You 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
Whitey

Whitey

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts

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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users