Vladimir
Vladimir

Reputation: 1789

MySQL- Selecting data from two tables and ordering by max values

I have 2 tables with different num rows and I have volumns vote_up this value is different for every row in 2 tables. I need to union 2 tables and order by max vote_up value. Here is my try, but get just 1 result:

SELECT name, vote_up 
FROM ( 
   SELECT name, vote_up 
   FROM comments 

   UNION ALL 

   SELECT name, vote_up 
   FROM replays ) T 
ORDER BY MAX(vote_up) DESC

Upvotes: 0

Views: 85

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93724

Looks like you just need to order the result in Vote_up DESC order and you don't want to filter any rows so remove Max from order by

SELECT name, vote_up 
FROM 
( 
SELECT name, vote_up 
FROM comments 
UNION ALL 
SELECT name, vote_up 
FROM replays
) T 
ORDER BY vote_up DESC

Upvotes: 1

Related Questions