tags:

views:

33

answers:

6

How would I go about selecting from one mysql table (friends) and ordering the results by fields in another table (users)?

The tables are setup as follows:

CREATE TABLE `users` (
  `id` int(12) NOT NULL auto_increment,
  `first_name` varchar(100) NOT NULL default '',
  `last_name` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

CREATE TABLE `friends` (
  `id` int(12) NOT NULL auto_increment,
  `user_id` int(2),
  `mutual` int(2) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

basically what I need to do is pull in the user_ids from the friends table, match them to the users table and get the users data, then order the output by the following:

concat(users.first_name, users.last_name) ASC, friends.mutual DESC
+2  A: 

You would use a JOIN to achieve this.

  SELECT * 
    FROM friends 
    JOIN users ON friends.user_id = users.id 
    ORDER BY concat(users.first_name, users.last_name) ASC, friends.mutual DESC
Chetan
Hi Chetan, I assumed that was the case, but alas, that is one of my weak areas, can you offer a bit more detail to guide for my particular case?
Jaime Cross
@Jaime: I was still editing my answer :)
Chetan
@Jaime: By the way, the question isn't clear as to whether you want to select data from the users table and order by the friends table, or select data from the friends table and order by the users table. My answer attends to the latter. If you wanted the former, just make it `SELECT * FROM users JOIN friends ...`
Chetan
Chetan, I modified what you posted first and it works well, thank you very much. I was needing to select the users data to display.
Jaime Cross
A: 

Select users.first_name, users.last_name, friends.mutual from users, friends where users.id = friends.id AND friends.mutual != 0;

May work, or you may need to tune it. I do not have env to give you exact query :)

Happy Mysqling :)

--Cheers

Koteswara sarma
@Koteswara: block your code then press ctrl+K
klox
Missing the ORDER BY
OMG Ponies
+1  A: 

Try this one! I think it should work!

SELECT
friends.user_id, users.*
FROM
friends, users
WHERE
  friends.user_id = users.id
ORDER BY
  concat(users.first_name, users.last_name) ASC, friends.mutual DESC
David Conde
Valid, but ANSI-89 syntax :/
OMG Ponies
Im going to regret this, but pride goes before the fall: What do u mean with ANSI-89 syntax? That I didnt made the JOIN?
David Conde
A: 

Hi. I believe you want something like:

SELECT * FROM users
INNER JOIN friends ON users.id=friends.user_id
ORDER BY CONCAT(users.first_name, users.last_name) ASC, friends.mutual DESC

I hope this helps!

Charles Hooper
A: 

use

select concat(users.first_name,users.last_name) as name,friends.mutual as friends from users 
right join friends on friends.users_id = users.id order by name asc, friends desc

This should do the job.

Saeros
A: 

you need read for detail inside this.

klox
Thanks klox, I have been studying it, just was not able to wrap my head around it fully yet :)
Jaime Cross
@Jaime: yupz..keep trying and learning.
klox