Php mysql中的快速和慢速移动产品

Php mysql中的快速和慢速移动产品,php,mysql,sql,xampp,Php,Mysql,Sql,Xampp,有谁能帮助我了解MySQL上快速和慢速移动的产品吗 我想知道如何SELECT子句将所有快速移动的产品和慢速移动的产品分开。这是我的桌子 **product** productID productname price **sales** transactionID productID quantity subtotal **transaction** transactionID datetransact 我剪了一些柱子使它看起来很简单 快速移动产品是在特定时间段内经常销售的产品。 慢速移动产

有谁能帮助我了解MySQL上快速和慢速移动的产品吗

我想知道如何
SELECT子句
将所有快速移动的产品和慢速移动的产品分开。这是我的桌子

**product**
productID
productname
price

**sales**
transactionID
productID
quantity
subtotal

**transaction**
transactionID
datetransact
我剪了一些柱子使它看起来很简单

快速移动产品
是在特定时间段内经常销售的产品。

慢速移动产品
是一种销售不太频繁且长期处于货架上的产品。

您需要按产品分组并选择最小值(datetransact)和最大值(datetransact)。这两个数字的差值将给出销售的产品数量以及第一个和最后一个销售日期之间的时间跨度。然后你可以除以这些得到一个平均值

更新以计算销售数量。

select sum(sales.quantity) as productssold,
       min(transaction.datetransact) as firstsale,
       max(transaction.datetransact) as lastsale,
       max(transaction.datetransact) - min(transaction.datetransact) as timespan,
       sum(sales.quantity) / max(transaction.datetransact) - min(transaction.datetransact) as averagesold
from product
join sales on product.productid = sales.productid
join transaction on sales.transactionid = transaction.transactionid
group by product.productid
having averagesold >= 'desired value'

斯科特的回答就其本身而言是好的。首先,您似乎关心的是销售产品的数量,而不仅仅是包含该产品的交易数量。而且,这个问题(可能已经修改)是关于一个特定的日期范围

要获得特定日期范围的答案,只需使用
where
子句或条件聚合即可。以下内容使用筛选并包括未销售的产品:

select p.*, sum(s.quantity) as productssold,
       sum(s.quantity) / datediff(@datelast, @datefirst)) as AvgPerDay
from product p left join
     sales s
     on p.productid = s.productid left join
     transaction t
     on s.transactionid = t.transactionid
where t.datetransact between @datefirst and @datelast
group by p.productid
order by AvgPerDay;
如果您不想要从未售出的产品,只需将
左连接
更改回内部连接即可

过滤方法的问题在于,某些产品可能在您的月经期开始后首次销售。要处理这个问题,您需要测量自第一个销售日期(或者可能自
产品
表中的某个发布日期)以来的平均值。这基本上将日期条件从
where
子句移动到
having
子句:

select p.*, sum(case when t.datetransact between @datefirst and @datelast then s.quantity else 0 end
               ) as productssold,
       (sum(case when t.datetransact between @datefirst and @datelast then s.quantity else 0 end) /
        datediff(@datelast, least(@datefirst, max(t.datetransact)))
       ) as AvgPerDay
from product p left join
     sales s
     on p.productid = s.productid left join
     transaction t
     on s.transactionid = t.transactionid
group by p.productid
order by AvgPerDay;

什么定义了“快速移动的产品”和“缓慢移动的产品”?我编辑我的帖子来解释它,希望你能理解。所以你想根据特定datetransact范围内的交易频率来选择产品?根据datetransact范围内的销售频率对它们进行排序。是的,先生!哇!这正是我所需要的,它计算在特定时间范围内售出的产品数量。它只需要像asc和desc一样快速和缓慢。谢谢先生!哦,等等,我怎样才能在第一次销售和最后一次销售中确定期望值?例如我想从上个月的2014年7月24日到2014年8月24日,从今天开始选择销售到2014年8月24日的顶级产品?先生,销售的产品有一个问题,它只计算销售表中出现的产品,而不是产品的实际销售数量-_-哇,谢谢你,老兄,第一段代码可以用作死锁。无法从datefirst和datelast开始销售的产品。你们很有帮助,这对其他人也很有用。非常感谢!等等,这里有个问题。如果我运行上述条款,我的销售表已售出5种产品。它在avgperday上返回空值,售出的商品返回0。@MharveenBiu。您是否定义了变量
@datefirst
@datelast
?如果这种情况持续存在,您能否在SQLFIDLE上设置一个示例。我不知道您在谈论什么,在mysql查询中定义变量。我还不是高级用户。但如果你能教我就好了。我是否要运行包含@datefirst和@datelast的sql?插入
@datefirst
@datelast
的日期。