5

I'm having the table

  • sales_orders

In this table having column names

  • order_no
  • order_status
  • order_type

SQL query:

SELECT order_no FROM sales_orders where order_status='Pending' and order_type='1'

In the above query if the order_type=1 value doesn't exists in the database column that means instead of value 1 there is a value '0' , I want to display an error.

How to modify the above query for that?

2
  • What type of error do you want? Do you want to get just a flag back if the pending order has an order_Type <>1? Commented Nov 21, 2012 at 10:16
  • if the order_type=1 is not existed in the database i want to display an error like "There is no rail order" in php Commented Nov 21, 2012 at 10:18

5 Answers 5

1

If I understand correctly, you want to show an error if order_type is not equal to 1. If so, you can do something like this:

$query = "SELECT order_no FROM sales_orders where order_status='Pending' and order_type = 1";

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_object()) {
        if ($row->order_type != 1) {
            printf('There is no rail order for order number %d', $row->order_no);
        }
    }

    $result->close();
}
Sign up to request clarification or add additional context in comments.

7 Comments

in the if condition it is not order_status, it order_type
Fixed the condition. Thanks! Speaking about 'displaying an error' - it's a rather abstract definition. You can print a string, you trigger a notice|warning|error or you can throw an exception. It's not really clear from the original question, what the author wants to do.
In the order_type column there will be either '1' or '0' only... when checking the condition in WHERE clause, if value '1' doesn't exists in the database, it should display an error in the screen like "There is no rail order for the customer you have selected"
Your answer is the best suited for my requirement. but it should display an error
The query's where clause is selecting for order_type = 1, so you'd never get back a row where $row->order_type != 1, would you?
|
0

try this:

SELECT IF(order_type=1,order_no,'Error') 
FROM sales_orders 
WHERE order_status='Pending'

Comments

0

The SQL should be then something like this (you can't use order_type=1 in the WHERE as you want to have the "errornous" results back also, thus you need to use an IF there):

SELECT IF(order_type=1,'OK','Error') AS okErrorFlag ,order_no
FROM sales_orders 
WHERE order_status='Pending'

In sql (I take now an associative array as a result where you have only the result of the appropriate row there):

if ($row['okErrorFlag']=='Error')
{
     echo 'ERROR: There is no rail order';
}

Comments

0

try using case statement

select id,
case order_type
    when '1' then order_type
    when '0' then 'error'
end 
from
`sales_orders`
where order_status='pending'

1 Comment

what about the order_status='pending'
0

Finally i got it what i need...

This is the query:

  • $sql_da_num= "SELECT order_no FROM sales_orders WHERE order_status='Pending' and order_type = '1');

                $result_da_num= db_query($sql_da_num);
                $num_rows = db_num_rows($result_da_num);
                if ($num_rows==0)
                {
                display_error("There is no Rail Order for this selected customer");
                }
                }
    

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.