5

我有桌子

  • 销售订单

在此表中具有列名

  • 订单号
  • 订单状态
  • 订单类型

SQL查询:

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

在上面的查询中,如果order_type=1数据库列中不存在该值,这意味着值 1 而不是值 '0' ,我想显示一个错误。

如何为此修改上述查询?

4

5 回答 5

1

如果我理解正确,如果 order_type 不等于 1,您希望显示错误。如果是这样,您可以执行以下操作:

$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();
}
于 2012-11-21T10:20:36.563 回答
0

然后 SQL 应该是这样的(你不能在 WHERE 中使用 order_type=1 ,因为你也希望返回“错误”结果,因此你需要在那里使用 IF ):

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

在 sql 中(我现在将一个关联数组作为结果,其中您只有相应行的结果):

if ($row['okErrorFlag']=='Error')
{
     echo 'ERROR: There is no rail order';
}
于 2012-11-21T10:21:22.143 回答
0

尝试使用 case 语句

select id,
case order_type
    when '1' then order_type
    when '0' then 'error'
end 
from
`sales_orders`
where order_status='pending'
于 2012-11-21T10:23:17.580 回答
0

尝试这个:

SELECT IF(order_type=1,order_no,'Error') 
FROM sales_orders 
WHERE order_status='Pending'
于 2012-11-21T10:17:34.187 回答
0

最后我得到了我需要的东西......

这是查询:

  • $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");
                }
                }
    
于 2012-11-21T12:52:59.467 回答