从两个不相关的表中获取记录

时间:2016-08-10 06:02:32

标签: mysql

我有一张场地和服务表。我想要从两个表中选择所有记录。两者都有不同的列。

服务表:

 Name   Type       Collation    Attributes  Null    Default Extra   Action
    1   serviceId  Primary  int(11)         No  None    AUTO_INCREMENT  Change 
    2   entry_by    varchar(255)    latin1_swedish_ci       No  None        
    3   service_name    varchar(255)    latin1_swedish_ci       No  None        
    4   service_address varchar(255)    latin1_swedish_ci       No  None        
    5   latitude    double          No  None        Change Change   Drop Drop   
    6   longitude   double          No  None        Change Change   Drop Drop   
    7   servicetypeId   int(11)         No  None        Change Change   Drop 
    8   active  int(11)         No  None        Change Change   Drop Drop   

场地表:

1   venueIdPrimary  int(11)         No  None    AUTO_INCREMENT  Change 
2   venue_name  varchar(255)    latin1_swedish_ci       No  None    
3   venuetypeId int(11)         No  None        Change Change   Drop Drop   
4   entry_by    varchar(255)    latin1_swedish_ci       No  None        
5   venue_address   varchar(255)    latin1_swedish_ci       No  None        
6   latitude    double          No  None        Change Change   Drop Drop   
7   longitude   double          No  None        Change Change   Drop Drop   
8   active  int(11)     

我尝试了这个查询:SELECT服务。,场地。 FROM servicesvenues

但它会提供重复记录。所有记录3次:

 1
Abc
DJ siddhi
nasik
1211
4545
1
1
1
Halll siddhi
1
Abc
nasik
19.975409
73.841321
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
1
Halll siddhi
1
Abc
nasik
19.975409
73.841321
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
1
Halll siddhi
1
Abc
nasik
19.975409
73.841321
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
2
decorator siddhi
2
Abc
nasik
19.974781
73.854094
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
2
decorator siddhi
2
Abc
nasik
19.974781
73.854094
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
2
decorator siddhi
2
Abc
nasik
19.974781
73.854094
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
3
siddhi hall
8
sid17
nasik
19.995005
73.841308
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
3
siddhi hall
8
sid17
nasik
19.995005
73.841308
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
3
siddhi hall
8
sid17
nasik
19.995005
73.841308
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
4
siddhi hall
8
sid17
Sankuru, Democratic Republic of the Congo
-2.8437453
23.3823545
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
4
siddhi hall
8
sid17
Sankuru, Democratic Republic of the Congo
-2.8437453
23.3823545
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
4
siddhi hall
8
sid17
Sankuru, Democratic Republic of the Congo
-2.8437453
23.3823545
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
5
siddhi cafe
8
sid17
Unnamed Road, Democratic Republic of the Congo
0
0
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
5
siddhi cafe
8
sid17
Unnamed Road, Democratic Republic of the Congo
0
0
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
5
siddhi cafe
8
sid17
Unnamed Road, Democratic Republic of the Congo
0
0
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
6
siddhi cafe
8
sid17
Lomami, Democratic Republic of the Congo
-1.899681
22.72851
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
6
siddhi cafe
8
sid17
Lomami, Democratic Republic of the Congo
-1.899681
22.72851
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
6
siddhi cafe
8
sid17
Lomami, Democratic Republic of the Congo
-1.899681
22.72851
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
7
green lawn
8
sid17
N5, Democratic Republic of the Congo
-10.4212157
28.6031842
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
7
green lawn
8
sid17
N5, Democratic Republic of the Congo
-10.4212157
28.6031842
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
7
green lawn
8
sid17
N5, Democratic Republic of the Congo
-10.4212157
28.6031842
1
1
Abc
DJ siddhi
nasik
1211
4545
1
1
8
hotel nashik
8
sid17
Mai-Ndombe, Democratic Republic of the Congo
-2.6357434
18.4276047
1
2
sid17
siddhi  cateror
Unnamed Road, Democratic Republic of the Congo
0
0
6
1
8
hotel nashik
8
sid17
Mai-Ndombe, Democratic Republic of the Congo
-2.6357434
18.4276047
1
3
sid17
flowerist
Unnamed Road, Democratic Republic of the Congo
-10.4212157
28.6031842
6
1
8
hotel nashik
8
sid17
Mai-Ndombe, Democratic Republic of the Congo
-2.6357434
18.4276047
1

如何从两个表中获取唯一记录?谢谢..

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

 select 
 'Services'      as Source       ,
 serviceId       as ID           ,
 entry_by        as entry_by     ,
 service_name    as name         ,
 service_address as address      ,
 latitude        as latitude     ,
 longitude       as longitude    ,
 servicetypeId   as typeID       ,
 active          as active    
 from services

 union

 select
 'Venues'         as Source      ,
 venueIdPrimary   as ID          ,
 entry_by         as entry_by    ,
 venue_name       as name        ,
 venue_address    as address     ,
 latitude         as latitude    ,
 longitude        as longitude   ,
 venuetypeId      as typeID      ,
 active           as active    
 from venues ;

请注意,我添加了一个文本字段,以便您知道记录的来源。