SQL从父表和子表中获取值

时间:2014-11-06 12:48:23

标签: sql-server

table 1                                                                    
.........

p_id | p_name| p_age|p_gender     

101  | Aniyan| 27   | male     


table2 
.......                                     

c_id |p_id | c_name |c_age | c_gender                                                 
201  | 101 | Devraj | 35   |  male                         
202  | 101 | Anisha | 20   | female                                      
203  | 101 | Deepak | 50   | male  

我需要得到这样的答案,

name  |  age  | gender                                  

Aniyan| 27    | male                           
Devraj| 35    | male                                                      
Anisha| 20    | female                                                           
Deepak| 50    | male                                                                

我需要合并表格......任何帮助都是可观的。表1包含作为监护人的主要人物。表2是同一预订的成员。我需要根据我的项目将它保存在不同的表中。所以我在等待...对不起,我没有多少声誉可以将其作为照片发布。

2 个答案:

答案 0 :(得分:0)

您需要使用当前定义的UNION

SELECT p_name AS Name, p_age AS Age, p_gender AS Gender FROM Table1
UNION
SELECT c_name AS Name, c_age AS Age, c_gender AS Gender FROM Table2

答案 1 :(得分:0)

这将结合两个表的结果

select p_name as name, p_age as age, p_gender as gender
from table1
union
select c_name as name, c_age as age, c_gender as gender
from table2