2016-06-20 85 views
1

我想從allinventory_tb獲取描述和模型。MySQL聯盟全部結合MySQL內部聯接

所以我做了內部連接,但是當我要顯示描述和模型時。

的錯誤是這樣的:

說明:未定義指數:說明,注意:未定義指數:模型。

有什麼建議嗎?

|allinventory_tb| 
    ---------------- 
    |in_code  | 
    |description | 
    |model   | 
    --------------- 
    $sql = "select t.itemcode as itemcode ,sum(t.qty) as qty 
    from ( 
    select itemcode,qty from barcode INNER JOIN allinventory_tb on barcode.itemcode = allinventory_tb.in_code 
    union all 
    select itemcode,qty from adjustment_tb INNER JOIN allinventory_tb on adjustment_tb.itemcode = allinventory_tb.in_code where adjustment_tb.status='APPROVED' 
    union all 
    select itemcode,(qty * -1) from soldout_pd INNER JOIN allinventory_tb on soldout_pd.itemcode = allinventory_tb.in_code) as t 
    group by itemcode"; 

    $result = $conn->query($sql); 
+0

你的錯誤看起來是由PHP,而不是MySQL的未來。原始查詢是否在MySQL中運行,沒有錯誤?控制檯給你什麼錯誤? –

+0

查詢運行時沒有錯誤,如果我要顯示allinvty_tb中的行,但是我已經做了INNER JOIN的話,錯誤將會顯示。 – codeSeven

+0

你可以_please_始終引用你的表名嗎?我僅在您的問題中看到_three_版本的名稱:allinventory_tb,allinvty3和allinvty_tb。 –

回答

1

有了您的查詢即

$sql = "select t.itemcode as itemcode ,sum(t.qty) as qty 
    from ( 
    select itemcode,qty from barcode INNER JOIN allinventory_tb on barcode.itemcode = allinventory_tb.in_code 
    union all 
    select itemcode,qty from adjustment_tb INNER JOIN allinventory_tb on adjustment_tb.itemcode = allinventory_tb.in_code where adjustment_tb.status='APPROVED' 
    union all 
    select itemcode,(qty * -1) from soldout_pd INNER JOIN allinventory_tb on soldout_pd.itemcode = allinventory_tb.in_code) as t 
    group by itemcode"; 


    $result = $conn->query($sql); 

您將無法訪問的描述和型號的列值,因爲你沒有在查詢中指定的願望列名,所以當您嘗試訪問查詢結果在PHP中,您將收到Notice Notice錯誤,例如:

注意:未定義索引:說明,注意:未定義索引:model。

嘗試此查詢

$sql = "select description,model,t.itemcode as itemcode ,sum(t.qty) as qty 
    from ( 
    select description,model,itemcode,qty from barcode as bc INNER JOIN allinventory_tb as ait on bc.itemcode = ait.in_code 
    union all 
    select description,model,itemcode,qty from adjustment_tb as adt INNER JOIN allinventory_tb as ait1 on adt.itemcode = ait1.in_code where adjustment_tb.status='APPROVED' 
    union all 
    select description,model,itemcode,(qty * -1) from soldout_pd as slp INNER JOIN allinventory_tb as ait2 on slp.itemcode = ait2.in_code) as t 
    group by itemcode"; 


$result = $conn->query($sql); 

希望這對你的作品...

+0

注意:試圖獲取非對象的屬性。我已經檢查過了。 – codeSeven

+0

@codeSeven可以根據上述查詢發佈您的var_dump()結果。我認爲你錯誤地訪問了它。 –

+0

我同意@FaisalMohmand請將您的var_dump()結果發佈 –