简体   繁体   English

如何从堆栈中的2个表中提取数据

[英]How to pull data from 2 table in stack

I know many the many people think i can use "join" to pull data from two table but that's not i want. 我知道许多人认为我可以使用“ join”从两个表中提取数据,但这不是我想要的。

actually it is kind of question to me, is it possible to pull data like this way here i am explaning :- 实际上,这对我来说是一个问题,是否有可能像我在这里这样解释:

i have two table 我有两个桌子

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

i want to pull data from both table but not i another column using join like left or right join is there any way to pull data in a new row where i with sorting rank in desc or asc order if yes then help me. 我想从两个表中提取数据,但是我不希望使用联接(如左联接或右联接)在另一列中提取数据到新行中,因此我可以按desc或asc的顺序对数据进行排序,如果可以,那么可以帮助我。

here is the result which i want 这是我想要的结果

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

Thanks in advance. 提前致谢。

try this: 尝试这个:

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

this pulls from both tables, and orders by column#5 descending, and column#1 ascending 这从两个表中提取,并按列#5降序和列#1升序排序

try use union all and order by rank and vid desc : 尝试使用union all并按rankvid desc rank

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM