php - MySQL Union 全部结合 MySQL Inner Join

标签 php mysql join union

我想从 allinventory_tb 获取描述和模型。

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

错误是这样的:

Notice: Undefined index: description , Notice: Undefined index: model.

有什么建议吗?

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

Notice: Undefined index: description , Notice: Undefined index: 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);

希望这对你有用...

关于php - MySQL Union 全部结合 MySQL Inner Join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915015/

相关文章:

php - sql server从两个表中获取字符串中的最大值整数

python - 根据来自其他两个 DataFrame 的索引合并两个 DataFrame

mysql - 在 Lync 2010 LCSCDR 数据库中,他们是一种判断谁接听了响应组调用的方法

php - 如何减少php中的月份?

php - 如何扩大PHP变量的范围?

php - ImageMagick 的 SVG 输出格式错误

相同varchar的Mysql总和

php - codeigniter 仅以用户 1 身份插入数据

mysql - 如何在 C# 中转义 mysql 字符串以便与 LIKE 一起使用

sql - "table A left outer join table B ON TRUE"是什么意思?