假设我有某种SELECT查询,它向我显示了10行。
有没有办法可以更改此查询,以便每次显示两行,总共给出20个?
此外,可以在SELECT中添加一些任意标识符以区分每对行吗?
答案 0 :(得分:2)
这应该有效:
select a, b, c, 1 as pair from myTable
union all
select a, b, c, 2 as pair from myTable
答案 1 :(得分:0)
是的,你可以使用null
这样做。跑吧。
UNION ALL
答案 2 :(得分:0)
详细说明@ cdaiga的答案并在每行中包含一个唯一标识符......
SET @i := 0;
SELECT <your columns>, @i := @i + 1 as num from ...
UNION ALL
SELECT <your columns>, @i := @i + 1 as num from ...
然后,您将拥有一个带有连续序列号的num
列。这是一个例子:fiddle。
答案 3 :(得分:0)
使用任意标识符选择两次列,然后使用union all
取消/转换为行例如给定
MariaDB [sandbox]> select fb_uid,id,username from users;
+--------+----+----------+
| fb_uid | id | username |
+--------+----+----------+
| 1 | 1 | John |
| 2 | 2 | Jane |
| 3 | 3 | Ali |
| 6 | 6 | Bruce |
| 7 | 7 | Martha |
| 8 | 8 | Sidney |
| 10 | 10 | charlie |
| 12 | 12 | Elisa |
| 14 | 14 | Samantha |
| 15 | 15 | Hannah |
| 16 | 16 | Hannah |
| 17 | 17 | Kevin |
| 18 | 18 | Kevin |
| 19 | 19 | Ruth |
+--------+----+----------+
14 rows in set (0.00 sec)
select fb_uid,id1 id,uname1 username
from
(
select fb_uid,id id1,username uname1, id id2, username uname2 from users u
) u1
union all
select fb_uid,id2,uname2
from
(select fb_uid,id id1,username uname1, id id2, username uname2 from users u
) u2
order by id
结果
+--------+----+----------+
| fb_uid | id | username |
+--------+----+----------+
| 1 | 1 | John |
| 1 | 1 | John |
| 2 | 2 | Jane |
| 2 | 2 | Jane |
| 3 | 3 | Ali |
| 3 | 3 | Ali |
| 6 | 6 | Bruce |
| 6 | 6 | Bruce |
| 7 | 7 | Martha |
| 7 | 7 | Martha |
| 8 | 8 | Sidney |
| 8 | 8 | Sidney |
| 10 | 10 | charlie |
| 10 | 10 | charlie |
| 12 | 12 | Elisa |
| 12 | 12 | Elisa |
| 14 | 14 | Samantha |
| 14 | 14 | Samantha |
| 15 | 15 | Hannah |
| 15 | 15 | Hannah |
| 16 | 16 | Hannah |
| 16 | 16 | Hannah |
| 17 | 17 | Kevin |
| 17 | 17 | Kevin |
| 18 | 18 | Kevin |
| 18 | 18 | Kevin |
| 19 | 19 | Ruth |
| 19 | 19 | Ruth |
+--------+----+----------+
28 rows in set (0.00 sec)