【问题标题】:How to sort the result of multiple queries alternatively?如何对多个查询的结果进行交替排序?
【发布时间】:2015-10-16 08:45:29
【问题描述】:

我有一个由三个 select 子句组成的查询,如下所示:

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 对我不起作用)

【问题讨论】:

  • 您有 3 个表,并且您正在为每个表选择 3 列,并让它们显示在一列上,您实际上需要为每个表设置 3 个并集。您能否提供一些示例数据以及表格?此外,当您选择无顺序时,它不能保证它们将以哪种顺序显示,就像您在第一个查询结果中一样 red, green, blue, yellow
  • @AbhikChakraborty 我的问题已更新。

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


【解决方案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,....

【讨论】:

  • 你的大脑值得称赞......!每次我看答案时,我都喜欢它。干得好朋友! +1
  • 只有一个问题:什么时候有用? @rn:=@rn+1 as id,为什么是as id?因为有一个真正的列名为id
  • 原因是上面的查询是从已经选择的数据中选择数据。现在,如果您注意到我们只选择了colorrn,这是我们为每个查询计算的行数,而外部选择查询只是根据所有选定行计算一些增量数字的另一种方法。如果我们碰巧在内部查询中选择了 id,那么您可能会得到类似 1,1,2,2,.... 等 id 的结果,看起来不太好。
猜你喜欢
  • 1970-01-01
  • 2022-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
相关资源
最近更新 更多