我有一个查询执行我需要的大部分内容,但不能为空结果提供表示逻辑。
考虑这个问题:
SELECT
SUM(order_detail.price/order_detail.qty*order_detail.qty_dispatched) AS orderedPrice,
orders.location,
orders.id,
suppliers.supplier_name,
DAYNAME(FROM_UNIXTIME(orders.datetime)) AS Day_Name,
from_unixtime(orders.datetime) AS Day_Date
FROM orders, order_detail, products,suppliers
WHERE (orders.datetime BETWEEN '$from_date' AND '$to_date'
AND order_detail.order_id = orders.id
AND orders.location LIKE '%$locations%'
AND order_detail.product_id=products.id
AND products.supplier_id IN ($suppliers)
AND suppliers.id IN ($suppliers)
AND products.supplier_id=suppliers.id
AND (orders.status <> 'deleted') )
如果我得到每周天的结果,我可以使用PHP发布处理结果并显示结果:
Location | Supplier Name | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Total
--------------------------------------------------------------------------
Alhambra | Widget Co | $50 | $50 | $50 | $50 | $50 | $50 | $50 | $350
但是,如果在任何一天没有订单,我显然没有结果,我无法找到一个方法来发布流程以在列中显示$ 0。
我想也许平日表可能是JOIN的答案,但我无法理解。像这样的东西?
CREATE TABLE week_days(
week_day_num INT(11) DEFAULT NULL
);
INSERT INTO week_days(week_day_num) VALUES (1),(2),(3),(4),(5),(6),(7);
SELECT
DAYNAME(FROM_UNIXTIME(orders.datetime)) AS day_name,
SUM(order_detail.price/order_detail.qty*order_detail.qty_dispatched) AS orderedPrice,
COALESCE(SUM(order_detail.price/order_detail.qty*order_detail.qty_dispatched) AS orderedPrice , 0)
FROM week_days wd
LEFT JOIN (
SELECT
orders.location,
orders.id,
suppliers.supplier_name,
from_unixtime(orders.datetime) AS Day_Date
FROM orders, order_detail, products,suppliers
WHERE (orders.datetime BETWEEN '$from_date' AND '$to_date'
AND order_detail.order_id = orders.id
AND orders.location LIKE '%$locations%'
AND order_detail.product_id=products.id
AND products.supplier_id IN ($suppliers)
AND suppliers.id IN ($suppliers)
AND products.supplier_id=suppliers.id
AND (orders.status <> 'deleted') )
) order_results
ON wd.week_day_num = DAYOFWEEK(FROM_UNIXTIME(orders.datetime))
GROUP BY
products.supplier_id,orders.location,DAYOFWEEK(FROM_UNIXTIME(orders.datetime))
ORDER BY location, products.supplier_id,Day_Date ASC;
答案 0 :(得分:0)
这里的语法正在变弱:
COALESCE(SUM(order_detail.price/order_detail.qty*order_detail.qty_dispatched) AS orderedPrice , 0)
AS
子句需要超出COALESCE
函数。
COALESCE(SUM(order_detail.price/order_detail.qty*order_detail.qty_dispatched), 0) AS orderedPrice