Php 查询一段时间内订单的百分比和订单数量
我有一个“userorders”表,其中包含orderID、userID、orderStatus(可以是1,2,3)和orderTime 我想计算过去6个月内,userid=1的订单状态为1的最后150个订单的百分比 我试图为两个订单状态(1,2/3)编写两个查询,然后计算订单的百分比,但我的查询不正确 我的代码和查询:Php 查询一段时间内订单的百分比和订单数量,php,mysql,count,percentage,Php,Mysql,Count,Percentage,我有一个“userorders”表,其中包含orderID、userID、orderStatus(可以是1,2,3)和orderTime 我想计算过去6个月内,userid=1的订单状态为1的最后150个订单的百分比 我试图为两个订单状态(1,2/3)编写两个查询,然后计算订单的百分比,但我的查询不正确 我的代码和查询: $rs1 = mysql_query("select count(*) as orderCount1 from userorders where order
$rs1 = mysql_query("select count(*) as orderCount1 from userorders where
orderStatus = 1 and orderID in (select top 150 orderID from userorders where
userid = 1 and orderStatus in (1,2,3) and
orderTime > ".strtotime("-6 month")." oder by orderID desc)") or
die (mysql_error());
$rs2 = mysql_query("select count(*) as orderCount1 from userorders where
orderStatus in (2,3) and orderID in (select top 150 orderID from userorders where
userid = 1 and orderStatus in (1,2,3) and
orderTime > ".strtotime("-6 month")." order by orderID desc)") or
die (mysql_error());
$orderCount1 = $rs1['orderCount1'];
$orderCount2 = $rs2['orderCount2'];
$orderPercent = ($orderCount1/ $orderCount1+$orderCount2)*100;
如何解决问题或改进代码。我找到了正确的查询 它有一个条件最多的主查询,订单百分比根据主查询的输出计算:
$rs = mysql_query("select (sum(case when orderStatus = 1 then 1 else 0 end)
/count(orderStatus))*100 as percentage from (select orderStatus from
userorders where orderStatus in (1,2,3) and userid = 1 and
orderTime > ".strtotime("-6 month")." order by orderID desc limit
150) tempTable") or die (mysql_error());
$percentage1 = $rs['percentage'];
对于orderStaus 2,3
$percentage2 = 100 - $percentage1;
您想在所有订单中找到orderStatus的百分比=1吗?@ethrbunny我检查了这个Q和A,对于答案,它返回50个数字,这是代理的平均值,但我想得到一个数字,这是orderStatus的平均值。@Visculis实际上orderStatus可以是1,2,3,。。。但在这种情况下,我想找到orderStatus的百分比,当它为1和2,3时。