在mysql的内部查询中使用group by

时间:2014-10-17 14:45:07

标签: mysql

这是我在子查询中的选择查询当我尝试使用组时,结果集不合适我希望使用group by stoneCompanyId,stoneSizeId

$z = 0;
$selectDia = "SELECT 
                 designmasterdetail.designMasterId,stonesize.stoneSizeId,stonesize,
                 dimandWei,dimandTot,orderQty,stoneCompanyName
               FROM orderdetail
                  LEFT JOIN designmasterdetail ON designmasterdetail.designMasterId
                                     = orderdetail.designMasterId
                  LEFT JOIN stonesize ON stonesize.stoneSizeId = 
                                       designmasterdetail.stoneSizeId
                  LEFT JOIN stonecompany ON stonecompany.stoneCompanyId = 
                                       designmasterdetail.stoneCompanyId
               WHERE orderMasterId = " . $orderMasterId;
$selectDiaRes = mysql_query($selectDia);
while ($row = mysql_fetch_array($selectDiaRes)) {
    $s = 0;
    $dsArray = array();
    $selctDiaQty = "SELECT  
                      orderQty,designmasterdetail.designMasterId,SUM(dimandQty) AS dimandQty
                   FROM orderdetail
                      LEFT JOIN designmasterdetail ON
                       designmasterdetail.designMasterId = orderdetail.designMasterId
                   WHERE orderMasterId = " . $orderMasterId . "
                   GROUP BY  stoneCompanyId , stoneSizeId";
    $selctDiaQtyRes = mysql_query($selctDiaQty);
    while ($dimandRow = mysql_fetch_array($selctDiaQtyRes)) {
        $dsArray['dimandQty'][$s] += $dimandRow['dimandQty'] *
                $dimandRow['orderQty'];
        $s++;
    }
}
$selectDiaRes = mysql_query($selectDia);
$o = 0;
while ($row = mysql_fetch_array($selectDiaRes)) {
    $stoneArray[$z]['stoneCompanyName'] = $row['stoneCompanyName'];
    $stoneArray[$z]['stonesize'] = $row['stonesize'];
    $stoneArray[$z]['dimandWei'] = $row['dimandWei'];
    $stoneArray[$z]['orderQty'] = $row['orderQty'];
    $stoneArray[$z]['dimandWei'] = $row['dimandWei'] * $row['orderQty'];
    $stoneArray[$z]['dimandQty'] = $dsArray['dimandQty'][$o];
    $stoneArray[$z]['dimandTot'] = $row['dimandTot'] * $row['orderQty'];
    $z++;
    $o++;
}

0 个答案:

没有答案