2012-06-20 77 views
1

我有一个句子表和一个词表。我正在尝试创建一个中间表,以便快速查找哪些句子包含单词。从现有表格创建多关系

Table A (sentences) 
id INT PRIMARY KEY 
sentence VARCHAR 

Table B (words) 
id INT PRIMARY KEY 
word VARCHAR 

Table C (relationship table) 
id INT PRIMARY KEY 
sentence_id INT 
word_id INT 

我可以填充SQL表C?

查询会经历词语B(字),每个字,记录是从A(句子)的句子通过使条目表C.

非常感谢,您的SQL学有专长。

回答

2

我会先从:

insert into table C (sentence_id, word_id) 
values (
select A.sentence_id, B.word_id from table B, table A 
where locate(word, sentence) > 0 
order by word, sentence) 
+0

是否“定位”在一个句子一个句子一个字区分,也是一个字的一部分。它与Sql服务器中的“LIKE”运算符类似吗? –

+0

有趣的是..我玩了一个加入a.sentence像CONCAT(“%”,b.word,“%”),但这似乎更清洁! – Ben

+0

您可能需要连接或子选择。我也玩过这个:'where location(word,sentence)> 0和IN IN(从B选择唯一字B2)'' –