MySQL,Select在Select Distinct中返回多个值
我正在尝试使用主表中的值创建一些表。精通PHP,但不精通MySQL。 主表包含以下列:MySQL,Select在Select Distinct中返回多个值,mysql,select,nested,Mysql,Select,Nested,我正在尝试使用主表中的值创建一些表。精通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 PRIM
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;
现在,我必须创建州、议会和城市,我已经尝试了两天与州的类似内容(我尝试了一些不同的代码):
但对于国家/地区id,此选择返回所有国家/地区id。
我只需要在Countries.iso与iso匹配的表Countries中获取国家id
在这个表格之后,州,我必须创建委员会,从地方获取值,再次选择Distinct,再次尝试从州获取州id,可能还有de国家id
求求你,谁能帮我找到正确的方法来筑巢?
谢谢。看来你需要加入
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
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