1
orderfood
orderfood_id   food_id   total_amount



foodcancel
foodcancel_id   food_id  status

$query = $this->db->query("SELECT * FROM order_food of LEFT JOIN `foodcancel` fc ON of.food_id = fc.food_id WHERE of.orderfood_id = '" . (int)$orderfood_id . "'");
    $order_foods = $query->rows;

above is my query, what i wanted is that if there food_id inside foodcancel table , exclude it from rows, possbile to do it ?

2 Answers 2

1

For exclude the existing values you could try checking null for corresponding matching value

    SELECT * 
    FROM order_food of 
    LEFT JOIN foodcancel fc ON of.food_id = fc.food_id 
         and  of.food_id  = your_value  

    WHERE fc.orderfood_id is  null 
        

anyway you should not php var in your sql code because in this way you are are risk for sqlinjection for avoid this you should take a look at prepared statement and binding param

Sign up to request clarification or add additional context in comments.

1 Comment

thanks , by following the code you provided , managed to get what i wanted
0

It's very possible to do. In my logic. first, you must get all food_id on food_cancel table. Then save it into variabel and use it when you show orderFood table with adding NOT IN condition.

I've write code for you,

<?php

// Get Food Id From Cancel 
$orderCancel   = mysqli_query($mysqli, "SELECT * FROM `foodcancel`");
$cancelId      = "";
while ($cancel = mysqli_fetch_array($orderCancel)) {
  $cancelId   .= $cancel["food_id"].",";
};
$cancelId      = substr($cancelId, 0, -1);

// Put Food Id on Cancel Table into NOT IN Condition Database
$orderFood     = mysqli_query($mysqli, "SELECT * FROM `orderfood` WHERE food_id NOT IN ($cancelId)");
while ($order  = mysqli_fetch_assoc($orderFood)) {
  $food[]      = $order;
};

echo json_encode($food);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.