2013-11-27 44 views
0

我有4個表來填充表3返回的數據如何聯接表

1. the first table(d_cities) for cities // related with the next table by country_id 
CityId |CountryID |RegionID |City |Latitude|Longitude  

2. the second table(d_country) for countries 
CountryId|Country 

3. the third table(ip2location_db11) for ip 
ip_from|ip_to |country_code|country_name| city_name  

4 the fourth table (ip_relation) would be like this 
CountryID |CityId|ip_from |ip_to 

我創建的第四個表來收集來自三個表自定義數據,並把它放在一個表.. 這將有一直秉乘:

通過ID加入(d_country,d_cities), 然後用IP表比較這名如果匹配 將取回的ID爲這些名稱& IPS匹配,並把它的第四個表 ..所以我寫這樣的代碼,需要支持修改此代碼

INSERT ip_relations (CountryID, CityId,ip_from,ip_to) 
SELECT * 
FROM d_cities 
INNER JOIN d_country ON d_cities.CountryID = d_country.CountryId 
INNER JOIN ip2location_db11 ON ip2location_db11.country_name = d_country.Country 
AND ip2location_db11.city_name = d_cities.City 

///這條SQL語句不行

回答

0
INSERT INTO ip_relations (CityId,CountryID,ip_from,ip_to) 
SELECT 
    d_cities.CityId, 
    d_cities.CountryID, 
    ip2location_db11.ip_from, 
    ip2location_db11.ip_to 
FROM d_cities 
INNER JOIN d_country ON d_cities.CountryID = d_country.CountryId 
INNER JOIN ip2location_db11 ON ip2location_db11.country_name = d_country.Country 
AND ip2location_db11.city_name = d_cities.City 
0
UPDATE ip_relations, ip2location_db11, d_cities, d_country 
set ip_relations.countryid =d_country.CountryID 
, ip_relations.cityid= d_cities.CityId 
, ip_relations.ip_from=ip2location_db11.ip_from 
, ip_relations.ip_to=ip2location_db11.ip_to 
WHERE ip_relations.countryID=d_country.countryID and   ip_relations.cityID=d_cities.CityID and ip_relations.ip_from=ip2location_db11.ip_from 
and ip_relations.ip_to=ip2location_db11.ip_to; 
+0

我想通過選擇三個表來更新第四張表 – user1080247

+0

好的,我已經編輯過它 –

+0

#1064 - 你的SQL語法錯誤;檢查與您的MySQL服務器版本對應的手冊,以便在第7行的'FROM d_country LEFT JOIN d_cities ON d_country.CountryId = d_cities.CountryID'附近使用正確的語法。 – user1080247

0

假設COUNTRY_ID,city_id,COUNTRYNAME等,通過了所有三個表是相同的:

SELECT 
,dco.countryid AS countryid 
,dci.cityid AS cityid 
,ip_from 
,ip_to 
FROM d_country dco 
INNER JOIN d_cities dci 
ON dco.countryid=dci.countryid 
INNER JOIN ip2location_db11 ip 
ON TRIM(dci.city)=TRIM(ip.city_name) 
AND TRIM(dco.country)=TRIM(ip.country_name) 
WHERE dco.country_id=ip.country_code 

UPDATE 
ip_relation 
FROM (
SELECT 
,dco.countryid AS countryid 
,dci.cityid AS cityid 
,ip_from 
,ip_to 
FROM d_country dco 
INNER JOIN d_cities dci 
ON dco.countryid=dci.countryid 
INNER JOIN ip2location_db11 ip 
ON TRIM(dci.city)=TRIM(ip.city_name) 
AND TRIM(dco.country)=TRIM(ip.country_name) 
WHERE dco.country_id=ip.country_code 
) DT 
SET 
ip_from=DT.ip_from 
ip_to=DT.ip_to 
WHERE CountryID=DT.countryid 
AND CityId=DT.cityid 
+0

我的問題是(如何更新從3連接表返回的數據表)..所以我們回答關於select不更新 – user1080247

+0

你能否檢查這是否是你的要求? –

+0

#1064-你的SQL語法有錯誤;檢查對應於你的MySQL服務器版本的手冊,選擇正確的語法以在'FROM(SELECT, dco.countryid AS countryid,dci.cityid AS cityid,ip_from,'在第3行 – user1080247

0

正確的加入方式是

UPDATE 
    ip_relations ir 
INNER JOIN ip2location_db11 idl ON ir.ip_to = idl.ip_to 
INNER JOIN ip2location_db11 idr ON ir.ip_from = idr.ip_from 
INNER JOIN d_cities dc ON ir.d_cities = dc.d_cities 
INNER JOIN d_country dct ON ir.countryID = dct.countryID 
SET 
    ir.CountryID = dct.CountryID, 
    ir.CityId  = dc.CityId, 
    ir.ip_from  = dcr.ip_from, 
    ir.ip_to  = dcl.ip_to 
// put where condition if required 

但是,當你加入一些密鑰並且想要更新密鑰時,我確信所有密鑰即使在更新後也是相同的,所以這將不起作用。如果你更新一些其他列,那麼它是實用的。
爲了解這個假設這個例子。

加入兩個表上的關鍵是3. 3來自第二表。用3更新第一個表的列。那麼爲什麼你需要這個?你需要更新其他一些不同於你加入的列。

+0

#1054 - 'on子句'中的未知列'ir.d_cities' – user1080247

+0

#1054 - '字段列表'中的未知列'dcr.ip_from' – user1080247

+0

我更新我的問題 – user1080247

1

第一,我不知道爲什麼你設計的表像this.maybe你可以改變他們象下面這樣:

d_cities: city_id | country_id | region_id |city | latitude| longitude 
d_country: country_id | country 
ip2location_db11: ip_from | ip_to | country_code | city_id 

PS:我不是很確定什麼呢COUNTRY_CODE的意思,所以我在上面的表結構上保留它,它大多是這樣的:國家到城市是一對多的,city_id必須是唯一的,ips只與city_id有關係。 我認爲,這將是一個更好的設計...

然後,如果你必須根據你當前的表格解決問題。 你會製作一個唯一的密鑰「UNIQUE KEY uniq_from_toip_from,ip_to)」;並且,有SQL:

INSERT IGNORE ip_relation 
(SELECT cA.country_id,cA.city_id,ip.ip_from,ip.ip_to FROM ip2location_db11 ip 
LEFT JOIN (SELECT cy.country,ct.city,ct.country_id,ct.city_id FROM d_country cy,d_cities ct WHERE cy.country_id = ct.country_id) as cA ON ip.city_name = cA.city AND ip.country_name = cA.country); 

這意味着:1.find所有城市國家集團;然後根據城市國家集團; 2.插入到你的來回表,當ip_from-ip_to是重複的,將覆蓋之前的數據。

希望這可以給你一些幫助。