I have these two tables above. The first table contains different drugs. The second table contains verified combinations of any two drugs from the first table.
By default there is a reference drug where drugID = $_GET['ref'] in the URL. Lets just say default.php?ref=3 for vaccine.
I want to list all drugs from the drugs table, except where drugID = $_GET['ref'] = 3. This will show all drugs except vaccine. This is done like this:
$ref = htmlspecialchars($_GET['ref']);
$sql = "SELECT * FROM drugs WHERE drugID != {$ref} ORDER BY drug ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["drug"];
}
}
For every drug in the loop (drugID) I want to check whether there exist a comboID where drugID and $_GET['ref'] are both found in either drug1_ID or drug2_ID. If such a comboID exist, echo "combination found:" . row['comboID'] . row["interaction"]. This should return 4 combinations with vaccine.
I have tried left, right, inner and full join of the tables, but nothing seems to work. How can this be done?
In the following example, I tried to left join:
SELECT *
FROM drugs
LEFT JOIN combination
ON (drugs.ID = combination.drug1_ID AND {$ref} = combination.drug2_ID) OR ({$ref} = combination.drug1_ID AND drugs.ID = combination.drug2_ID);
This just returns every drug, but more than once if combination is found.