I have a table of dictionary terms and now I would like to create another table which would hold ID of the ‘main’ term and ID of related terms that have the ‘main’ term’s Name included in their Definition –
table: terms
| id | name | definition |
Table: related
| term_id | related_term_id |
It was pretty easy to create INSERT statements in PHP:
$result = mysql_query("SELECT * FROM terms");
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
$result2 = mysql_query("SELECT id,name FROM terms WHERE description LIKE \"%$name%\" AND id!=".$row['id']);
while ($row2 = mysql_fetch_array($result2)) {
echo "INSERT INTO related(term_id,related_term_id) VALUES(".$row['id'].",".$row2['id'].");";
}
}
However, as I don’t have too much experience with MySQL I was wondering if that can be done purely in SQL – let say with use of INSERT-SELECT statement?
Thanks!
1 Answer