PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE `pivot` ( id INT AUTO_INCREMENT, table_1_id INT, table_2_id INT, PRIMARY KEY (id) ); INSERT INTO `pivot` (id, table_1_id, table_2_id) VALUES (1, 1, 3), (2, 2, 3), (3, 1, 4), (4, 5, 6), (5, 3, 6), (6, 7, 8), (7, 4, 6), (8, 8, 9), (9, 4, 7), (10, 2, 9), (11, 1, 9);
Copy Clear
Copy Format Clear
<?php // $mysqli is already declared as a working database connection by this sandbox $table1Ids = [1, 3, 5, 3]; $table2Ids = [2, 6, 2]; $uniqueIds1 = array_unique($table1Ids); $uniqueIds2 = array_unique($table2Ids); $count1 = count($uniqueIds1); $count2 = count($uniqueIds2); $placeholders1 = implode(',', array_fill(0, $count1, '?')); $placeholders2 = implode(',', array_fill(0, $count2, '?')); $stmt = $mysqli->prepare("SELECT * FROM `pivot` $sql WHERE table_1_id NOT IN ($placeholders1) AND table_2_id NOT IN ($placeholders2)"); $stmt->bind_param(str_repeat('i', $count1 + $count2), ...$uniqueIds1, ...$uniqueIds2); $stmt->execute(); $result = $stmt->get_result(); echo json_encode($result->fetch_all(), JSON_PRETTY_PRINT);
Show:  
Copy Clear