0

I have two queries one will return data ordered by likes and in the user city the other one return data by the distance . so if query 1 return : id 1,2,3 (order by likes) and query 2 return : id 4,5,6 (order by distance) i need the final set results to be 1,2,3,4,5,6 i've tried to do union between the two queries but it's not working. any other suggestions ?

2 Answers2

0

You can use left join or union according to this link. Union ALL also works like you can see here.

Example: SELECT 1 UNION ALL SELECT 2

Community
  • 1
  • 1
Keurdange
  • 1
  • 2
0

the solution was to put a limit to each query then the union will work correct : (SELECT DISTINCT ID, 'a' as type,... FROM table1 GROUP BY ID ORDER BY likesDESC limit 50) union all( SELECT DISTINCT ID, 'b' as type,....FROM table1 GROUP BY ID ORDER BY distance limit 50) order by type asc.