2016-06-09 25 views
0

我想使用mysql将表中的值复制到另一个表中,同时通过第三个表循环来设置特定值第二。mysql将数据库1中的数据库行多次复制到新数据库2,循环访问数据库3

表1被调用的国家,与结构和数据:

 
countries 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ countries_id + countries_name + countries_iso_code_2 + countries_iso_code_3 + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
+ 1   + Belgium  + BE     + BEL     + 
+ 2   + Netherlands + NL     + NLD     + 
+ 3   + Germany  + DE     + DEU     + 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

中的行countries_id和countries_name需要被复制到表countries_name。对于来自表语言的每个language_id。 表2

 
countries_name 
+++++++++++++++++++++++++++++++++++++++++++++++ 
+ countries_id + language_id + countries_name + 
+++++++++++++++++++++++++++++++++++++++++++++++ 
+ 1   + 1   + Belgium  + 
+ 2   + 1   + Netherlands + 
+ 3   + 1   + Germany  + 
+ 1   + 3   + Belgium  + 
+ 2   + 3   + Netherlands + 
+ 3   + 3   + Germany  + 
+ 1   + 4   + Belgium  + 
+ 2   + 4   + Netherlands + 
+ 3   + 4   + Germany  + 
+++++++++++++++++++++++++++++++++++++++++++++++ 

表3

 
languages 
+++++++++++++++++++++++++++++++++ 
+ languages_id + name + code + 
+++++++++++++++++++++++++++++++++ 
+ 1   + English + en + 
+ 3   + Dutch + nl + 
+ 4   + German + de + 
+++++++++++++++++++++++++++++++++ 

我知道如何为单次做到这一点,但并不适用于多个。

CREATE TABLE countries_name (

countries_id int(11) NOT NULL,

language_id int(11) NOT NULL DEFAULT 1,

countries_name varchar(64) NOT NULL,

UNIQUE countries (countries_id, language_id),

KEY idx_countries_name_zen (countries_name)

) ENGINE=MyISAM;

INSERT INTO countries_name (countries_id, countries_name)

SELECT c.countries_id, c.countries_name

FROM countries c;

+0

所以,P浏览你的代码,你是如何做到这一点的? – Alex

+0

加入单程的代码 – Zen4All

回答

0

http://sqlfiddle.com/#!9/bd3c7/1

如果我得到了正确的你的目标:

INSERT INTO countries_name (countries_id, language_id, countries_name) 
SELECT 
c.countries_id, 
l.languages_id, 
c.countries_name 
FROM countries c 
LEFT JOIN languages l 
ON 1; 
+0

谢谢,但那只是第一种语言,现在查询需要循环其余的语言,并且再次用其他语言编号的 – Zen4All

+0

复制这些名字?你在说什么?你试过这个查询吗? – Alex

+0

当然,我做:)查询只复制一次,只为了language_id 1,而不是3和4 – Zen4All