根据标准对数据求和

时间:2015-03-20 04:04:01

标签: php mysql sql

我的数据库 here 包含多个表格:

我想通过商店获得stok_in个产品和stok_sisa 将最后一个输入id和id与具有相同产品的不同商店相加。

我在PHP上的SQL查询编程如下:

$sql1="
    SELECT 
    master_outlet.NAMA_OUTLET, 
    master_outlet.ID_OUTLET,
    ID_PRODUK, SUM(STOK_IN)AS TOTAL_IN 

    FROM 
    master_outlet, 
    tb_stok_outlet_produk 

    WHERE 
    master_outlet.ID_OUTLET = 
    tb_stok_outlet_produk.ID_OUTLET 
    GROUP BY ID_PRODUK";
$sqlquery1= mysqli_query($mysqli, $sql1);

while ($data1=mysqli_fetch_assoc($sqlquery1))
{
    //echo "ini query1". $sql1."</br>";

    echo $data1[ID_PRODUK]."&nbsp;"; echo $data1[TOTAL_IN]."<BR>";
    /*
    $sql2= "
    SELECT NAMA_PRODUK, STOK_SAAT_INI  
    FROM tb_produk, tb_input_detail 
    WHERE 
    tb_input_detail.ID_PRODUK = '$data1[ID_PRODUK]' 
    AND tb_produk.ID_PRODUK = tb_input_detail.ID_PRODUK 
    AND ID_INPUT_DETAIL = 
    ( 
        SELECT MAX( ID_INPUT_DETAIL ) 
            FROM tb_input_detail 
            WHERE ID_PRODUK = '$data1[ID_PRODUK]'  
    )";
    */

    $sql2= "
    SELECT 
    master_produk.NAMA_PRODUK, 
    master_outlet.NAMA_OUTLET,

    SUM( tb_input_detail.STOK_SAAT_INI ) AS STOK_SAAT_INI

    FROM 
    master_produk, 
    master_outlet, 
    tb_input_detail


    WHERE 

    tb_input_detail.ID_PRODUK = master_produk.ID_PRODUK
    AND tb_input_detail.ID_OUTLET = master_outlet.ID_OUTLET
    AND tb_input_detail.ID_PRODUK = '$data1[ID_PRODUK]'
    AND tb_input_detail.STOK_SAAT_INI = 
    (
        SELECT SUM( tb_input_detail.STOK_SAAT_INI )
        FROM tb_input_detail
        WHERE master_outlet.ID_OUTLET = '$data1[ID_OUTLET]'
            AND tb_input_detail.ID_INPUT_DETAIL = 
            (
                SELECT MAX( tb_input_detail.ID_INPUT_DETAIL )
                FROM tb_input_detail 
            ) 
    )
            GROUP BY tb_input_detail.ID_PRODUK
    LIMIT 0 , 30";

但结果不正确。 我很困惑,也尝试了其他几个问题。

0 个答案:

没有答案