doudou1309 2014-05-08 12:56
浏览 74
已采纳

如何从MYSQL中的COUNT(*)ORDER BY语句打印数组的内容

How would I print the contents of the following array :

$sql=
"
SELECT shop_order_location, COUNT(*) FROM shop_orders
LEFT JOIN product
ON shop_orders.shop_order_product_id=product.product_id
WHERE 
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '2014-04-01' and '2014-05-01' AND brand = '226' AND shop_order_action = 'Sale'
GROUP BY shop_order_location";

$total = mysql_fetch_array(mysql_query($sql));

...

This query generates the following table

Bawtry      36
Chain       10
Grantham    10
Internet    14

I want to pass each integer result into a separate PHP variable, i.e. $Bawtry = 36, $Chain=10 etc. and then print each value separately.

Thanks

* UPDATE *

<?
$sql=
"
SELECT shop_order_location, COUNT(*) FROM shop_orders
LEFT JOIN product
ON shop_orders.shop_order_product_id=product.product_id
WHERE 
DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
BETWEEN '2014-04-01' and '2014-05-01' AND brand = '226' AND shop_order_action = 'Sale'
GROUP BY shop_order_location";

$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)){
   ${$row['shop_order_location']} = $row['COUNT'];


}
?>

This code in it's current state isn't appearing to do anything - when I run the php script, it returns a black page.

I'm trying to pass the contents of the array to separate variables, i.e.

$Bawtry = 36 $Chain = 10

So how would I separate out the results of the array and pass them into separate variables?

UPDATE 2

/*
*   Overall array by shop location counting total pairs
*/

$sql = "SELECT shop_order_location
             , COUNT(*) AS TOTAL
        FROM shop_orders
        LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
        WHERE
    DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
        BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
        AND shop_order_action = 'Sale'

    GROUP BY shop_order_location";

    $result1 = mysql_query($sql);
    while ($overall = mysql_fetch_array($result1)){
    ${$overall['shop_order_location']} = $overall['TOTAL'];
    } 
/*
*   Mens array - counting pairs of brand 
*/

$sql2 = " SELECT shop_order_location
             , COUNT(*) AS TOTAL
        FROM shop_orders
        LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
        WHERE   
    DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
        BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
        AND shop_order_action = 'Sale' AND category = 'Mens'

    GROUP BY shop_order_location";

    $result2 = mysql_query($sql2);
    while ($mens = mysql_fetch_array($result2)){
    ${$mens['shop_order_location']} = $mens['TOTAL'];
    }
/*
*   Ladies array - counting pairs of brand
*/  

$sql3 = " SELECT shop_order_location
             , COUNT(*) AS TOTAL
        FROM shop_orders
        LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
        WHERE  
    DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
        BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
        AND shop_order_action = 'Sale' AND category = 'Ladies'

    GROUP BY shop_order_location";

    $result3 = mysql_query($sql3);
    while ($ladies = mysql_fetch_array($result3)){
    ${$ladies['shop_order_location']} = $ladies['TOTAL'];
    }
/*
*   SUM query - sales total cost
*/

$sql4 = "SELECT SUM(shop_order_price)
        FROM shop_orders
        LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
        WHERE  
    DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
        BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."' AND shop_order_action = 'Sale'";

$result4 = mysql_query($sql4) or die(mysql_error());

So could the array results be passed like so :

while($row4 = mysql_fetch_array($result4)){
        ?>

        <?
        $showtotal = $row4['SUM(shop_order_price)'] ;
        $showaverage = $row4['SUM(shop_order_price)'] / $total ;
        ?>
                <tr>
                    <td align="center"><strong><?=$brand['name'];?></strong></td>
                    <!-- Bawtry Mens figure -->
                    <td align="center"><?=$mens['Bawtry'];?></td>
                    <!-- Bawtry Ladies figure -->
                    <td align="center"><?=$ladies['Bawtry'];?></td>
                    <!-- Overall Bawtry Figure -->
                    <td align="center"><?=$overall['Bawtry'];?></td>   
  • 写回答

1条回答 默认 最新

  • dongluo9156 2014-05-08 12:58
    关注
    $sql = "SELECT shop_order_location
                 , COUNT(*) AS TOTAL
            FROM shop_orders
            LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
            WHERE DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
                  BETWEEN '2014-04-01' and '2014-05-01' AND brand = '226' 
                  AND shop_order_action = 'Sale'
            GROUP BY shop_order_location";
    
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)){
       ${$row['shop_order_location']} = $row['TOTAL']; 
    }
    echo $Bawtry; //Check assignment
    echo ${'Bawtry'}; //It is better to access your variables this way in case there are strings with spaces in the 'shop_order_location' column
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?