我尝试使用主表中的值创建一些表。 PHP专家但不是MySQL专家。 主表有以下列:
Table Places
ISO
Country
Language
Region2 (is the estate)
Region4 (is ths city council)
ID (id for locality)
Locality
获得国家并不困难:
CREATE TABLE countries ( id integer(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
iso varchar(2) NOT NULL, language varchar(2) NOT NULL,
name varchar(50) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO countries ( iso , language, name)
SELECT DISTINCT ISO AS iso, Language as language, Country AS name FROM Places WHERE 1;
现在我必须建立国家,理事会和城市,而且我已经和国家一起尝试了两天这样的事情(我已尝试过不同的代码):
CREATE TABLE states ( id integer(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
country_id integer(11) UNSIGNED NOT NULL, country_iso varchar(2) NOT NULL,
name varchar(80) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO states (country_id, country_iso, name)
SELECT DISTINCT
(SELECT countries.id AS country_id from countries WHERE countries.iso = Places.ISO),
ISO as country_iso ,
Region2 AS name FROM Places WHERE 1;
但是对于country_id,此选择会返回所有国家/地区ID。 我只需要在表格中获得国家ID,其中countries.iso与地方的ISO匹配。
在此表之后,各州,我必须创建理事会,从地方获取值,再次使用选择功能,并再次尝试从州获取州ID,也可能是国家ID。
拜托,任何人都可以通过严格的方式让我对这个选项进行嵌套吗? 感谢。
答案 0 :(得分:1)
似乎你需要加入
INSERT INTO states (country_id, country_iso, name)
SELECT DISTINCT countries.countries.id , ISO, Places.Region2
from countries
inner join Placesc on countries.iso = Places.ISO