2017-02-24 76 views
1

我知道许多人认为我可以使用“连接”从两个表中提取数据,但那不是我想要的。如何从堆栈中的2个表中提取数据

实际上它是一种问题对我来说,有可能拉像这里这样 我正在地名释义数据: -

我有两个表

+-------------------------------------+    +-------------------------------------+ 
|    Table 1    |    |    Table 2    | 
+-------------------------------------+    +-------------------------------------+ 
|id |vid| name | about  | rank |    |id |vid| title | description | rank | 
+-------------------------------------+    +-------------------------------------+ 
| 1 | a | dsa | Lorem ipsu | 0  |    | 1 | b | aa | Lorem ipsu | 0  | 
| 2 | a | asda | adipisicin | 0  |    | 2 | b | ss | adipisicin | 0  | 
| 3 | a | da | tempor inc | 0  |    | 3 | b | dd | tempor inc | 0  | 
| 4 | a | sad | dolore mag | 2  |    | 4 | b | rr | dolore mag | 2  | 
| 5 | a | fd | quis nostr | 1  |    | 5 | b | ggf | quis nostr | 2  | 
| 6 | a | rewr | ullamco la | 2  |    | 6 | b | ffdd | ullamco la | 1  | 
| 7 | a | ewrr | consequat. | 2  |    | 7 | b | df | consequat. | 1  | 
| 8 | a | dsa | reprehende | 1  |    | 8 | b | ddf | reprehende | 1  | 
| 9 | a | fffd | cillum dol | 1  |    | 9 | b | dfd | cillum dol | 1  | 
| 10| a | fsd | Excepteur | 1  |    | 10| b | df | Excepteur | 2  | 
+-------------------------------------+    | 11| b | dff | proident, | 2  | 
                | 12| b | trr | deserunt m | 2  | 
                +-------------------------------------+ 

我想拉从数据这两个表,但不是我另一列使用连接像左或右连接有没有什么办法拉动数据在一个新的行,我与排序排名desc或asc顺序如果是的话帮助我。

这里是结果,我想提前

+-----------------------------------------+ 
|    Result Table    | 
+-----------------------------------------+ 
| id | vid | name | about  |rank | 
+-----------------------------------------+ 
| 4 | b | rr | dolore mag | 2 | 
| 5 | b | ggf | quis nostr | 2 | 
| 10 | b | df | Excepteur | 2 | 
| 11 | b | dff | proident, | 2 | 
| 12 | b | trr | deserunt m | 2 | 
| 4 | a | sad | dolore mag | 2 | 
| 6 | a | rewr | ullamco la | 2 | 
| 7 | a | ewrr | consequat. | 2 | 
| 6 | b | ffdd | ullamco la | 1 | 
| 7 | b | df | consequat. | 1 | 
| 8 | b | ddf | reprehende | 1 | 
| 9 | b | dfd | cillum dol | 1 | 
| 5 | a | fd | quis nostr | 1 | 
| 8 | a | dsa | reprehende | 1 | 
| 9 | a | fffd | cillum dol | 1 | 
| 10 | a | fsd | Excepteur | 1 | 
| 1 | b | aa | Lorem ipsu | 0 | 
| 2 | b | ss | adipisicin | 0 | 
| 3 | b | dd | tempor inc | 0 | 
| 1 | a | dsa | Lorem ipsu | 0 | 
| 2 | a | asda | adipisicin | 0 | 
| 3 | a | da | tempor inc | 0 | 
+-----------------------------------------+ 

感谢。

+0

检查mysql union如何工作。有了它,你将能够获得这样的数据 – xurca

回答

1

尝试使用union all和order by rankviddesc

SELECT id,vid,name,about,rank 
FROM(select id,vid,name,about,rank 
from table1 
UNION ALL 
select id,vid,title as name,description as about,rank 
from table2) t 
order by rank desc,vid desc 
1

试试这个:

select * from table1 
UNION 
select * from table2 
ORDER BY 5 DESC, 1 

这是直接从两个表,并和订单经柱#5降下来,列#1升