我有数据库列
表名:
最初,当数量分派的trans_details表被更新时,数据将被插入到trans_details表中。
销售栏
trans_details中的列
我想显示所有的值:- ordered_quantity - dispatched_quantity - pending_quantity
SELECT
IF(trans.ordered_quantity!='',trans.ordered_quantity,(sorder.total_quantity)) AS quantity,
IF(trans.dispatched!='',trans.dispatched,0) AS today_dispatched_qty,
IF(trans.dispatched!='',trans.dispatched,0) AS dis_qty,
IF(trans.Pending_quantity!='',trans.Pending_quantity,sorder.total_quantity) AS pending_qty
FROM
sales as sorder
LEFT OUTER JOIN trans_details as trans 该查询工作正常,但当数量完全发送它应该'0‘,但现在它显示的是total_quantity.在这种情况下,当我用'0‘代替sorder.total_quantity时,IF(trans.Pending_quantity='0',trans.Pending_quantity,sorder.total_quantity) AS pending_qty.最初它显示的是'0‘,但它应该显示total_quantity.
样本输出:
total_quantity..........dispatched_quantity.......pending_quantity
50 45 5
5 5 0
5 0 5发布于 2012-12-20 14:58:12
我猜您的数据中有空值。如果问题是NULL,并且数据类型是数字的,那么尝试如下:
SELECT coalesce(trans.ordered_quantity,sorder.total_quantity) AS quantity,
coalesce(trans.dispatched,0) AS today_dispatched_qty,
coalesce(trans.dispatched,0) AS dis_qty,
coalesce(trans.Pending_quantity,sorder.total_quantity) AS pending_qty 如果这些确实是字符串,那么您需要添加一个空检查。我鼓励您使用case,这是标准SQL,而不是if。
select (case when trans.ordered_quantity is not null and trans.ordered_quantity <> ''
then trans.ordered_quantity
else sorder.total_quantity
end) as quantity,
. . .最后,我假设您只是意外地忽略了on条款。在除MySQL之外的任何数据库中,您都会得到一个解析错误。但是,作为一种好习惯,在指定内部或外部联接时,应该始终使用on子句。
https://stackoverflow.com/questions/13965735
复制相似问题