2014-06-23 89 views
0

我有一个表单:ID;名称Mysql插入时选择

1-dog 
2-log 
3-for 

然后我有一个表中的字符:ID; NAME

1-_og 
2-_or 
3-_ot 

现在我有一个thirth关系表(让我们称之为它words_and_characters),我希望把从表中的单词和ID IDS从“匹配”表中的字符。 由ID,WORD_ID,CHARACTER_ID

在上面的例子由表ID,表words_and_characters应包含以下:

1-1-1 (the match is "dog" and "_og") 
2-2-1 (the match is "log" and "_og") 
3-3-2 (the match is "for" and "_or") 

我在表中开始写入与第一光标的存储过程对于iteratin根据这个字符:

select id, name from characters; 

,并把日期是这样的:

FETCH curs1 INTO i,n; 

现在,在每次迭代中,我想这样做:

select * from words where name like n; 

好,恐怕这是一个有点无效的,尤其是如果有大量的数据。 有没有更好的方式来做到这一点你认为?也许我也可以避免编写一个存储过程,只是有一些预付款SQL插入?

回答

0

使用JOIN:

INSERT INTO words_and_characters (word_id, character_id) 
SELECT w.id, c.id 
FROM words AS w 
JOIN characters AS c ON w.name LIKE c.name