2016-06-20 58 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()结果发布 –