如何交替排序多个查询的结果?

时间:2015-10-16 08:45:29

标签: mysql sql-order-by union-all

我有三个select id, colors from table1 union all select id, numbers from table2 union all select id, names from table3 子句的查询,如下所示:

// 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    |
                       +----+---------+

此处还有表结构:

+----+--------+
| 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   |
+----+--------+

现在我想要这个结果的顺序:

order by 1,2,3

我该如何实现? (应该注意,mEditText.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { new Thread(new Runnable() { @Override public void run() { try { session.typing(); } catch (OmegleException e) { e.printStackTrace(); }catch(NullPointerException e){ e.printStackTrace(); } } }).start(); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(final CharSequence s, final int start, int before, int count) { new Thread(new Runnable() { @Override public void run() { try { if(start==-1){ session.stopTyping(); } } catch (OmegleException e) { e.printStackTrace(); }catch(NullPointerException e){ e.printStackTrace(); } } }).start(); } }); 对我不起作用)

1 个答案:

答案 0 :(得分:1)

这就是你如何做到这一点

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 ;

我们的想法是为每个表项分配一个rn值,并确保这些值始终按升序排列

因此,如果您为每个表运行查询,那么

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)

您可以在此处看到table1 rn值为1,2,3,.... table2值为1.5,2.5,3.5,.... table3值为1.6,2.6,....

所以最后当你用所有的rn命令结果时它将是

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