0

orderID、userID、orderStatus (1、2、3)、および orderTime を持つ「userorders」のテーブルがあります。

orderStatus 1 を持つ userid = 1 について、過去 6 か月間の最後の 150 件の注文の割合を計算したいと考えています。

両方の注文ステータス (1、2/3) に対して 2 つのクエリを作成してから、注文の割合を計算しようとしましたが、クエリが正しくありません。

私のコードとクエリ:

$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;

問題を解決したり、コードを改善するにはどうすればよいですか。

4

1 に答える 1

0

正しいクエリを見つけます。

ほとんどの条件を持つ 1 つのメイン クエリがあり、注文の割合はメイン クエリの出力から計算されます。

$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'];

注文用ステータス2,3

$percentage2 = 100 - $percentage1;
于 2012-11-03T22:14:15.283 に答える