jfur7
jfur7

Reputation: 77

Only pull values that have not been inserted into a linked table

I am needing to pull vehicle infomration into a combo box and to do this I am using the following function:

function populateVehicles($mysqli) {
    $query = "SELECT * FROM vehicles";
    $result = $mysqli->query($query);
    while($row = $result->fetch_array()){
        echo "<option value=\"" . $row['idvehicle'] . "\">" . $row['make'] . " " . $row['model'] . " - Stock No: " . $row['stocknum'] . "</option>\n";
    }
}

But I have a table that links sales and vehicles called vehsale that has dual primary keys of saleid and vehicleid. How can I pull only those vehicles that have not been inputed into that table?

I'm thinking pull that info to the array and then just put an if statement in the while loop?

Upvotes: 0

Views: 36

Answers (1)

Fabio
Fabio

Reputation: 23500

You can try to find data you need directly with one query using left join function

 $query = "SELECT * FROM vehicles a
           LEFT JOIN vehsale b
           ON a.idvehicle = b.idvehicle 
           WHERE b.idvehicle IS NULL"; 

Please note that i supposed your id is idvehicle, you might change it to fit your needs.

Upvotes: 1

Related Questions