简体   繁体   English

PHP-MySQL每周销售报告查询问题

[英]PHP - MySQL Weekly Sales Report Query Issue

I am preparing weekly sales report for particular seller so he can see after login at any time. 我正在为特定卖方准备每周销售报告,以便他可以在登录后随时查看。

Current week info is collected using below code. 使用以下代码收集当前周信息。

<?php 
$dt_week_start_date = date('Y-m-d 20:00:01',strtotime("last Saturday"));
$dt_week_end_date = date('Y-m-d 20:00:00',strtotime("next Saturday"));
?>

Below query is used to get sold item data for specific seller from table for current week. 下面的查询用于从当周表中获取特定卖家的已售商品数据。 This query works fine if I simply display sold item listing on page. 如果我仅在页面上显示已售商品清单,则此查询工作正常。

<?php
$str_query_select = "SELECT * FROM t_product_purchase ";
$str_query_select .= " WHERE purchasedatetime BETWEEN '".$dt_week_start_date ."' AND '".$dt_week_end_date."'";
$str_query_select .= " AND sellerpkid=1";
$str_query_select .= " ORDER BY purchasedatetime DESC ";
?>

But I need to show sold items order wise like below. 但是,我需要像下面一样明智地显示已售出的商品订单。 I tried to do GROUP BY on purchasedatetime field and tried few other things but it's not working at all. 我尝试在purchateatetime字段上进行GROUP BY,并尝试了其他一些操作,但它根本无法正常工作。 Please help me to solve this isse. 请帮助我解决这个问题。

Purchase Date-Time       Product      Price
2014-08-10 14.20.00      Item 010      $50
2014-08-10 14.20.00      Item 016      $20
                         ------------------
                         Total :       $70
                         + Shipping :  $10
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $75


2014-08-13 09.08.10      Item 056      $20
2014-08-13 09.08.10      Item 056      $65
2014-08-13 09.08.10      Item 056      $ 5
                         ------------------
                         Total :       $90
                         + Shipping :  $15
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $100

Total For week 2014-08-09 to 2014-08-16 : $175

Data are stored in database table in following way. 数据按以下方式存储在数据库表中。 All data are related to single seller only. 所有数据仅与单一卖家有关。

  • Buyer 01 buys 2 items together from site where shipping charge is $10 & promo code discount is $5 (so 2 records for 2 items are added in t_product_purchase table along with shipping charge and promo code discount) 买家01从网站上一起购买了2件商品,运费为10美元,促销代码折扣为5美元(因此2件商品的2条记录与运费和促销代码折扣一起添加到t_product_purchase表中)

  • Buyer 02 buys 3 items together from site where shipping charge is $15 & promo code discount is $5 (so 3 records for 3 items are added in t_product_purchase table along with shipping charge and promo code discount) 买家02从网站上一起购买3件商品,运费为15美元,促销代码折扣为5美元(因此,t_product_purchase表中将3条商品的3条记录以及运费和促销代码折扣一起添加到网站中)

Here is database table structure: 这是数据库表结构:

CREATE TABLE IF NOT EXISTS `t_product_purchase` (
`purchasepkid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`sellerpkid` bigint(20) unsigned NOT NULL DEFAULT '0',
`productpkid` bigint(20) unsigned NOT NULL DEFAULT '0',
`prodcatpkid` bigint(20) unsigned NOT NULL DEFAULT '0',
`extendedprice` double(10,2) NOT NULL DEFAULT '0.00',
`shippingvalue` double(10,2) NOT NULL DEFAULT '0.00',
`discountpercentage` float(6,2) NOT NULL DEFAULT '0.00',
`discountamount` float(6,2) NOT NULL DEFAULT '0.00',
`finaldiscountedamount` float(6,2) NOT NULL DEFAULT '0.00',
`quantity` smallint(6) NOT NULL DEFAULT '0',
`color` varchar(255) DEFAULT NULL,
`size` varchar(255) DEFAULT NULL,
`emailid` varchar(255) NOT NULL DEFAULT '',
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`state` varchar(100) DEFAULT NULL,
`country` varchar(100) DEFAULT NULL,
`zipcode` varchar(10) DEFAULT NULL,
`shippingaddress` text,
`specialnote` text,
`ipaddress` varchar(50) DEFAULT NULL,
`purchasedatetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`purchaseday` int(11) DEFAULT '0',
`purchasemonth` int(11) DEFAULT '0',
`purchaseyear` int(11) DEFAULT '0',
PRIMARY KEY (`purchasepkid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

You can modify your query to get the Gross price while you do the other additions like the shipment and deductions like the Promo Code from within PHP. 您可以修改查询以获取毛价,同时在PHP中执行其他添加(例如装运)和扣除(例如促销代码)。

Here is what I mean: 这是我的意思:

<?php
$str_query_select = "SELECT *, SUM(price_column) as gross_price FROM t_product_purchase ";
$str_query_select .= " WHERE purchasedatetime BETWEEN '".$dt_week_start_date ."' AND '".$dt_week_end_date."'";
$str_query_select .= " AND sellerpkid=1";
$str_query_select .= " GROUP BY purchasedatetime ORDER BY purchasedatetime DESC ";
?>

You can now do your additions and deductions: PLEASE NOTE: You must use the column that is unique for a particular transaction for your grouping if purchasedatetime is not. 现在,您可以进行加法和减法:请注意:如果不包含purchateatetime,则必须使用特定交易唯一的列进行分组。

Purchase Date-Time                     Price
2014-08-10 14.20.00      Gross Total : $70
                         + Shipping :  $10
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $75


2014-08-13 09.08.10      Gross Total : $90
                         + Shipping :  $15
                         - Promo Code :$ 5
                         ------------------
                         Sub Total :   $100

Total For week 2014-08-09 to 2014-08-16 : $175

As an aside, isn't this easier to read?... 顺便说一句,这难道不容易阅读吗?

$str_query_select = "
SELECT * FROM t_product_purchase 
WHERE purchasedatetime BETWEEN '$dt_week_start_date' AND '$dt_week_end_date'
AND sellerpkid=1 
ORDER BY purchasedatetime DESC;
";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM