I've got an array of names:
$names = array('ray'=>0,'bob'=>1,'sue'=>2,'jeff'=>3);
Then I have a table that stores relationships between each keyword in the array:
+----------+----------+
| id_a | id_b |
+----------+----------+
| 0 | 1 |
+----------+----------+
| 0 | 2 |
+----------+----------+
| 0 | 3 |
+----------+----------+
| 1 | 2 |
+----------+----------+
| 1 | 3 |
+----------+----------+
| 2 | 3 |
+----------+----------+
At the moment my function to store the relationships is:
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);
}
I'm wondering if there is a faster SQL solution to handle this type of action?
You can build the inserted values array first, then insert it all with a single query ; that'd be faster. The query would look like...
INSERT INTO relationships (id_a, id_b) VALUES (0, 1), (0, 2), (0, 3);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.