我有一系列名称:
$names = array('ray'=>0,'bob'=>1,'sue'=>2,'jeff'=>3);
然后我有一个表来存储数组中每个关键字之间的关系:
+----------+----------+
| id_a | id_b |
+----------+----------+
| 0 | 1 |
+----------+----------+
| 0 | 2 |
+----------+----------+
| 0 | 3 |
+----------+----------+
| 1 | 2 |
+----------+----------+
| 1 | 3 |
+----------+----------+
| 2 | 3 |
+----------+----------+
目前我存储关系的功能是:
foreach($names as $name=>$id_a){
foreach($names as $n2=>$id_b){
if($name != $n2){
INSERT INTO relationships (id_a,id_b) VALUES ($id_a,$id_b);
}
}
array_shift($names);
}
我想知道是否有更快的 SQL 解决方案来处理此类操作?