tags:

views:

28

answers:

1

How can I add an order by users_friends.date_created that governs both select queries.

Here is my MySQL code.

(SELECT A.user_id, A.friend_id, B.username, B.avatar
FROM  users_friends AS A 
INNER JOIN users AS B 
ON A.friend_id = B.user_id 
AND A.user_id = 3 
AND A.friendship_status = 1)
UNION
(SELECT A.friend_id, A.user_id, B.username, B.avatar
FROM users_friends AS A 
INNER JOIN users AS B 
ON B.user_id = A.user_id 
AND A.friend_id = 3 
AND A.friendship_status = 1)
LIMIT 10
A: 

You can add an outer select with an order by and wrap the union as a derived table

SELECT user_id, friend_id, username, avatar FROM (
(SELECT A.user_id, A.friend_id, B.username, B.avatar
FROM  users_friends AS A 
INNER JOIN users AS B 
ON A.friend_id = B.user_id 
AND A.user_id = 3 
AND A.friendship_status = 1)
UNION
(SELECT A.friend_id, A.user_id, B.username, B.avatar
FROM users_friends AS A 
INNER JOIN users AS B 
ON B.user_id = A.user_id 
AND A.friend_id = 3 
AND A.friendship_status = 1)
LIMIT 10
) C
ORDER BY avatar
David Mårtensson
this dont work i get an error No database selected
blahit
It should work if the original query works.
David Mårtensson
I got it to work on my own but I'll give you credit for helping me thanks since this answer would probably help someone else.
blahit