通过组合其他两个行来创建一个Mysql数据库

时间:2015-02-03 11:03:14

标签: mysql sql xampp

我告诉你这个问题。我需要一个Mysql查询来创建Database3,拥有这两个数据库。

DATABASE1:
Url,Company Name,Address,City,ZipCode
www.aaa.com,AAA,StreetAAA,CityAAA,00000
www.bbb.com,BBB,StreetBBB,CityBBB,11111
www.ccc.com,CCC,StreetCCC,CityCCC,22222
www.ddd.com,DDD,StreetDDD,CityDDD,33333
www.eee.com,EEE,StreetEEE,CityEEE,44444
www.fff.com,FFF,StreetFFF,CityFFF,55555

DATABASE2:
Url,Phone
www.aaa.com,0000000000
www.aaa.com,1111111111
www.aaa.com,2222222222
www.bbb.com,3333333333
www.ccc.com,4444444444
www.ccc.com,5555555555
www.ccc.com,6666666666
www.ddd.com,7777777777
www.ddd.com,8888888888
www.eee.com,9999999999

我会这样的最终数据库,包括PHONE,重复所有电话号码的完整信息,不包括没有电话号码的行:

DATABASE3:
Url,Company Name,Address,City,ZipCode,PHONE
www.aaa.com,AAA,StreetAAA,CityAAA,00000,0000000000
www.aaa.com,AAA,StreetAAA,CityAAA,00000,1111111111
www.aaa.com,AAA,StreetAAA,CityAAA,00000,2222222222
www.bbb.com,BBB,StreetBBB,CityBBB,11111,3333333333
www.ccc.com,CCC,StreetCCC,CityCCC,22222,4444444444
www.ccc.com,CCC,StreetCCC,CityCCC,22222,5555555555
www.ccc.com,CCC,StreetCCC,CityCCC,22222,6666666666
www.ddd.com,DDD,StreetDDD,CityDDD,33333,7777777777
www.ddd.com,DDD,StreetDDD,CityDDD,33333,8888888888
www.eee.com,EEE,StreetEEE,CityEEE,44444,9999999999

提前致谢,

此致

2 个答案:

答案 0 :(得分:1)

您可以尝试以下

SELECT distinct
b.Url, a.Company Name, a.Address, a.City, a.ZipCode, b.Phone
from 
DATABASE2 b  left outer join DATABASE1 a
On a.Url = b.Url
Where b.Phone IS NOT NULL
Order by b.Phone

答案 1 :(得分:0)

MySQL支持create table ... as select,它根据查询创建一个新表:

create table NewTable as
select  t1.Url
,       t1.`Company Name`
,       ...
,       t2.Phone
from    table1 t1
left join
        table2 t2
on      t1.url = t2.url