How to sort the result of multiple queries alternatively?

与世无争的帅哥 提交于 2019-12-13 19:46:24

问题


I have a query made of three select clause like this:

select id, colors from table1
    union all
select id, numbers from table2 
    union all
select id, names from table3

Also here is the tables structure:

// table1               // table2              //table3
+----+--------+        +----+---------+        +----+-------+
| id | colors |        | id | numbers |        | id | names |
+----+--------+        +----+---------+        +----+-------+
| 1  | red    |        | 1  | ten     |        | 1  | jack  |
| 2  | green  |        | 2  | two     |        | 2  | peter |
| 3  | blue   |        | 3  | one     |        +----+-------+
| 4  | yellow |        | 4  | three   |
+----+--------+        | 5  | six     |
                       | 6  | five    |
                       +----+---------+

Now I want this order for the results:

+----+--------+
| id | colors |
+----+--------+
| 1  | red    |
| 2  | ten    |
| 3  | jack   |
| 4  | green  |
| 5  | two    |
| 6  | peter  |
| 7  | blue   |
| 8  | one    |
| 9  | yellow |
| 10 | three  |
| 11 | six    |
| 12 | five   |
+----+--------+

How can I implement that? (it should be noted, order by 1,2,3 does not work for me)


回答1:


This is how you can do this

select @rn:=@rn+1 as id,colors from (
  (select @rn1:= @rn1+1 as rn,colors from table1,(select @rn1:=0)x order by id )
   union all 
  (select @rn2:= @rn2+1 as rn,numbers as colors from table2,(select @rn2:=0.5)x order by id)
   union all 
  (select @rn3:= @rn3+1 as rn,names as colors from table3,(select @rn3:=0.6)x order by id )
)x,(select @rn:=0)y order by rn ;

The idea is to assign a rn value for each table item and need to make sure that these values are always in ascending order

So if you run the query for each table you will have

mysql> select @rn1:= @rn1+1 as rn,colors from table1,(select @rn1:=0)x order by id;
+------+--------+
| rn   | colors |
+------+--------+
|    1 | red    |
|    2 | green  |
|    3 | blue   |
|    4 | yellow |
+------+--------+
4 rows in set (0.00 sec)

mysql> select @rn2:= @rn2+1 as rn,numbers as colors from table2,(select @rn2:=0.5)x order by id;
+------+--------+
| rn   | colors |
+------+--------+
|  1.5 | ten    |
|  2.5 | two    |
|  3.5 | one    |
|  4.5 | three  |
|  5.5 | six    |
|  6.5 | five   |
+------+--------+
6 rows in set (0.00 sec)

mysql> select @rn3:= @rn3+1 as rn,names as colors from table3,(select @rn3:=0.6)x order by id;
+------+--------+
| rn   | colors |
+------+--------+
|  1.6 | jack   |
|  2.6 | peter  |
+------+--------+
2 rows in set (0.00 sec)

Here you can see table1 rn values are 1,2,3,.... table2 values are 1.5,2.5,3.5,.... table3 values are 1.6,2.6,....

so finally when you order the result with all rn it will be as

1,1.5,1.6,2,2.5,2.6,....



来源:https://stackoverflow.com/questions/33166093/how-to-sort-the-result-of-multiple-queries-alternatively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!