I have two tables:
我有两个表:
Table A: ID Cars Planes Num
1 3 5 2
2 8 44 1
3 7 23 6
4 6 2 7
Table B: ID Horses Dogs Cats Elefants Num
1 3 5 2 3 3
2 8 44 1 22 4
3 7 23 4 14 8
4 6 2 3 15 5
What I need to do: I need to get all results from both tables and sort them by the "Num" Column, where the "number" actually is unique for each result from both rows.
我需要做的是:我需要从两个表中获取所有的结果,并将它们按“Num”列排序,其中“number”实际上是由这两行的每个结果所特有的。
Is it even possible to "merge" those two tables and order them by "num" or should I just get each table separately ordered and do two loops checking always for the next num jumping between tables?
是否有可能“合并”这两个表,并通过“num”命令它们,或者,我是否应该让每个表分别排序,并做两个循环检查,以便在表之间的下一个num跳转?
Thanks
谢谢
2 个解决方案
#1
2
you can merge them like that with UNION .
你可以把它们合并到一起。
try this:
试试这个:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
演示
EDIT:
编辑:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
演示与其他列
#2
0
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1
#1
2
you can merge them like that with UNION .
你可以把它们合并到一起。
try this:
试试这个:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
演示
EDIT:
编辑:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
演示与其他列
#2
0
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1