Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL Union all与MySQL内部联接相结合_Php_Mysql_Join_Union - Fatal编程技术网

Php MySQL Union all与MySQL内部联接相结合

Php MySQL Union all与MySQL内部联接相结合,php,mysql,join,union,Php,Mysql,Join,Union,我想从allinventory\u tb获取描述和模型 所以我做了内部连接,但当我要显示描述和模型时 错误如下: $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 al

我想从
allinventory\u tb
获取描述和模型

所以我做了内部连接,但当我要显示描述和模型时

错误如下:

$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);
注意:未定义索引:描述,注意:未定义索引:模型

有什么建议吗

  |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);

使用您的查询,即

$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中访问查询结果时,您将收到如下错误通知:

注意:未定义索引:描述,注意:未定义索引:模型

尝试此查询

$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);

希望这对您有用…

您的错误看起来像是来自PHP而不是MySQL。原始查询在MySQL中运行时是否没有错误?控制台会给你什么错误?查询正在运行,没有错误,如果我要在allinvty_tb中显示该行,则会显示错误,尽管我已经做了内部联接。你能否始终引用表名?仅在您的问题中,我就看到了该名称的三个版本:
allinventory\u tb
allinvty3
allinvty\u tb
。抱歉。我的代码已经更新。注意:正在尝试获取非对象的属性。我已经检查过了。@codeSeven你能根据上面的查询发布你的var_dump()结果吗。我认为您访问它是错误的。我同意@FaisalMohm,请发布您的var_dump()结果