Php 从商店数据库中获取每日数据摘要
我在标准商店中有以下表格: id始终是主键,自动递增,ts始终是类型时间戳,在更新当前时间戳时更新 表销售: Sales是父表,userID与进行销售的用户相关。total和tendered均为浮动式。标志的类型为VARCHAR,可以是自由顺序 表格收入: 收据为销售的每种独特产品类型保留一行。oID是INT类型,与表sales的id相关。pID为INT类型,与表产品的id相关 表产品: Products是数据库中每个产品的中心数据。这是一条火星酒吧的线路。cID与表类别中的id相关 表类别 Categories是包含关于每个类别的所有数据的表,可以包含与一行相关的多个产品。显示类型为INT,指示类别何时启用或禁用1='true' 我的问题是,我想输出如下信息:Php 从商店数据库中获取每日数据摘要,php,mysql,sql,Php,Mysql,Sql,我在标准商店中有以下表格: id始终是主键,自动递增,ts始终是类型时间戳,在更新当前时间戳时更新 表销售: Sales是父表,userID与进行销售的用户相关。total和tendered均为浮动式。标志的类型为VARCHAR,可以是自由顺序 表格收入: 收据为销售的每种独特产品类型保留一行。oID是INT类型,与表sales的id相关。pID为INT类型,与表产品的id相关 表产品: Products是数据库中每个产品的中心数据。这是一条火星酒吧的线路。cID与表类别中的id相关 表类别 C
**Snacks**
(name) (quantity) (price) (total)
Fruit 3 50p £1.50
Twix 1 60p 60p
Boost 1 60 60p
**Hot Drinks**
(name) (quantity) (price) (total)
English Tea 15 60p £9.00
Speciality Teas 2 60p £1.20
我对其使用以下SQL:
SELECT categories.name AS category, products.name, pID,
(SELECT SUM(quantity) FROM receipts WHERE pID=r.pID AND DATE(ts) = CURDATE()) AS quantity,
products.price,r.ts
FROM receipts r
LEFT JOIN products ON r.pID = products.id
LEFT JOIN categories ON products.cID = categories.id
WHERE DATE(r.ts) = CURDATE()
GROUP BY r.pID
ORDER BY categories.name;
这似乎给了我正确的信息,但我不是100%确定。如果有人能证实这一点,我将不胜感激。但是,当我想看到某一天时,我会通过以下SQL得到不寻常的数据:
$postfrom = $_POST['from_mm']."/".$_POST['from_dd']."/20".$_POST['from_yy'];
$postto = $_POST['to_mm']."/".$_POST['to_dd']."/20".$_POST['to_yy'];
$from = strtotime($postfrom . " 6:00");
$to = strtotime($postto . " 23:59");
$itemised = select("SELECT categories.name AS category, products.name, pID,
(SELECT SUM(quantity) FROM receipts WHERE pID = r.pID AND UNIX_TIMESTAMP(r.ts) > '{$from}' AND UNIX_TIMESTAMP(r.ts) < '{$to}')
AS quantity, products.price
FROM receipts r
LEFT JOIN products ON r.pID = products.id
LEFT JOIN categories ON products.cID = categories.id
WHERE UNIX_TIMESTAMP(r.ts) > '{$from}'
AND UNIX_TIMESTAMP(r.ts) < '{$to}'
GROUP BY r.pID
ORDER BY categories.name;");
函数“select”只返回SQL表的一个数组。问题是,我可以通过在PHP中循环并以这种方式相加来轻松找到结果。但是我知道这在SQL中是可能的,我只是不知道为什么它不工作。有人能帮忙吗
编辑SQL示例fiddle在这里:尽管由于8000个字符的限制,我不能处理超过半天的数据。试试这个:
SELECT categories.name AS category, products.name AS name,
receipts.quantity AS quantity, products.price AS price,
(receipts.quantity * products.price) AS total
FROM categories
JOIN products
ON categories.id = products.cID
JOIN receipts
ON receipts.pID = products.ID
WHERE DATE(receipts.ts) = CURDATE()
ORDER BY categories.name
关于日期限制,您可以使用。。。和指定日期和时间。使用绝对日期和时间时刻或相对于当前日期和时间的时刻,例如DATEreceipts.ts介于concatcurdate-5、'6:00:00 AM'和curdate-4之间,而不是所有这些,只需提供适当的DDL,以及所需的结果和您迄今为止所做的最大努力。关于可疑的弃用函数和未转义数据的常见警告也许您可以提供一个SQLFIDLE,其中包含一些示例数据和所需的结果。@我真的不知道DDL引用的是什么。我认为提供尽可能多的信息有助于解决这个问题。我会的,而且我正在创建和处理SQL FIDLE。要将DDL(不管是什么意思)减少到8000个字符是非常困难的。
**Snacks**
(name) (quantity) (price) (total)
Fruit 3 50p £1.50
Twix 1 60p 60p
Boost 1 60 60p
**Hot Drinks**
(name) (quantity) (price) (total)
English Tea 15 60p £9.00
Speciality Teas 2 60p £1.20
SELECT categories.name AS category, products.name, pID,
(SELECT SUM(quantity) FROM receipts WHERE pID=r.pID AND DATE(ts) = CURDATE()) AS quantity,
products.price,r.ts
FROM receipts r
LEFT JOIN products ON r.pID = products.id
LEFT JOIN categories ON products.cID = categories.id
WHERE DATE(r.ts) = CURDATE()
GROUP BY r.pID
ORDER BY categories.name;
$postfrom = $_POST['from_mm']."/".$_POST['from_dd']."/20".$_POST['from_yy'];
$postto = $_POST['to_mm']."/".$_POST['to_dd']."/20".$_POST['to_yy'];
$from = strtotime($postfrom . " 6:00");
$to = strtotime($postto . " 23:59");
$itemised = select("SELECT categories.name AS category, products.name, pID,
(SELECT SUM(quantity) FROM receipts WHERE pID = r.pID AND UNIX_TIMESTAMP(r.ts) > '{$from}' AND UNIX_TIMESTAMP(r.ts) < '{$to}')
AS quantity, products.price
FROM receipts r
LEFT JOIN products ON r.pID = products.id
LEFT JOIN categories ON products.cID = categories.id
WHERE UNIX_TIMESTAMP(r.ts) > '{$from}'
AND UNIX_TIMESTAMP(r.ts) < '{$to}'
GROUP BY r.pID
ORDER BY categories.name;");
SELECT categories.name AS category, products.name AS name,
receipts.quantity AS quantity, products.price AS price,
(receipts.quantity * products.price) AS total
FROM categories
JOIN products
ON categories.id = products.cID
JOIN receipts
ON receipts.pID = products.ID
WHERE DATE(receipts.ts) = CURDATE()
ORDER BY categories.name