gpt4 book ai didi

php - 在没有数据的情况下返回设定值

转载 作者:行者123 更新时间:2023-11-29 14:08:16 25 4
gpt4 key购买 nike

我有一份报告,其中显示了正在销售的产品以及订购的每个产品的总重量。以下是显示总重量的代码标记:

$weightsql = 'select op.products_name, sum(op.products_quantity * p.products_weight) as weightsum from ' . TABLE_ORDERS_PRODUCTS . ' op left join ' . TABLE_PRODUCTS . ' p on op.products_id = p.products_id where op.products_id =  '.$pro['products_id'];
$weightq = tep_db_query( $weightsql );
while ($weight = tep_db_fetch_array( $weightq )){
if($category_parent_id != 0)$list_items[] = $weight['weightsum'];
}

它返回已订购产品的值,但对于未订购的产品,它保持空白,这非常令人困惑。对于未订购的产品(未从查询中提取的产品),我如何返回 0.00。

示例:

 Product Name         |             Total Weight
|
Jelly Beans | 25.00
Soft Candy | This product has not been ordered, needs to show 0.00
Bubblegum | 10.00

最佳答案

您正在寻找coalesce功能。

coalesce( sum(op.products_quantity * p.products_weight), 0 )

此外,将左联接更改为右联接,因为在您的查询中,产品位于订单的右侧。

这会将 Null 值替换为 0

已编辑应有的OP评论。您可以添加具有所需样式的新列:

select 
op.products_name,
coalesce( sum(op.products_quantity * p.products_weight), 0 ) as weightsum,
case when sum(op.products_quantity * p.products_weight) is NULL then 'Normal'
else 'Bold' end
as stype
...

关于php - 在没有数据的情况下返回设定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com