多查询PHP

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (55)

我需要帮助你的代码,多查询,(3查询)

查询1确定,查询2:错误查询3:错误

码:

$query ="select id from res_partner where supplier =true and vat='$emirfct1[0]'";

$query2 ="select substring(value_reference from position(',' in value_reference)+1)::int4 account_id from ir_property where name = 'property_account_expense' and res_id = 'product.template,' || 294";

$query3 ="select supplier,employee from res_partner  where  id=$ac_id";




$result = pg_query($query);
$result2 = pg_query($query2);
$result3 = pg_query($query3);
echo $result;

if(pg_numrows($result) )
{
    if( $row = pg_fetch_assoc($result) )
        {
            $iderp= $row['id'];

    }
}

if(pg_numrows($result2) )
{
    if( $row2 = pg_fetch_assoc($result2) )
    {
        $ac_id= $row2['account_id'];

    }
}

if(pg_numrows($result3) )
{
    if( $row3 = pg_fetch_assoc($result3) )
    {
        $supplier= $row3['supplier'];
        $employee= $row3['employee'];
    }
}

代码中有什么问题?

我有时间寻找解决方案,但我找不到它,我希望得到你的帮助,你可以引导我找到解决方案

提问于
用户回答回答于

$ac_id从第二个查询的结果设置变量之前,您尝试使用该变量。

您需要在此之后进行$query3分配和查询。

$query ="select id from res_partner where supplier =true and vat='$emirfct1[0]'";

$query2 ="select substring(value_reference from position(',' in value_reference)+1)::int4 account_id from ir_property where name = 'property_account_expense' and res_id = 'product.template,' || 294";

$result = pg_query($query);
$result2 = pg_query($query2);
$result3 = null;

if(pg_numrows($result) )
{
    if( $row = pg_fetch_assoc($result) )
    {
        $iderp= $row['id'];

    }
}

if(pg_numrows($result2) )
{
    if( $row2 = pg_fetch_assoc($result2) )
    {
        $ac_id= $row2['account_id'];
        $query3 ="select supplier,employee from res_partner  where  id=$ac_id";
        $result3 = pg_query($query3);
    }
}

if($result3 && pg_numrows($result3) )
{
    if( $row3 = pg_fetch_assoc($result3) )
    {
        $supplier= $row3['supplier'];
        $employee= $row3['employee'];
    }
}

您还可以使用这两个查询组合成单个查询 JOIN

select supplier, employee
from res_partner r
join join ir_property i on r.id = substring(i.value_reference from position(',' in i.value_reference)+1)::int4
where i.name = 'property_account_expense' and i.res_id = 'product.template,' || 294

扫码关注云+社区

领取腾讯云代金券