如果两个变量与PHP

时间:2015-06-26 13:20:06

标签: php mysql

我有两个变量$ faceplateInstalled和$ faceplateExists我将它们打印成两列

Faceplate Exists    Faceplate Instaled  
1                           1   
1                           0   
1                           1   
0                           0   
1                           1   
1                           0   
1                           1   
1                           1   
0                           0   
0                           0   
1                           0 

有没有办法删除我在同一行中有1和1的条目?

这是我使用的查询:

"SELECT F.id AS faceplateId, PR.premiseId, ComposeAddressFromPAF( PR.postAddressId ) AS fullAddress, " .
            "   IF( F.id IS NOT NULL, 1, 0 ) AS faceplateExists, " .
            "   IFNULL( F.installed, 0 ) AS faceplateInstalled, " .
            "   PR.siteId, S.NAME AS siteName " .
            "FROM ho_crm_premise PR " .
            "   LEFT JOIN ho_faceplate F ON F.premiseId = PR.premiseId " .
            "   INNER JOIN bcube_site S ON S.SITEID = PR.siteId " .
            "WHERE S.SITEID";

这是整个功能

function GetFaceplates( $siteId )
    {
        $sSql =
            "SELECT F.id AS faceplateId, PR.premiseId, ComposeAddressFromPAF( PR.postAddressId ) AS fullAddress, " .
            "   IF( F.id IS NOT NULL, 1, 0 ) AS faceplateExists, " .
            "   IFNULL( F.installed, 0 ) AS faceplateInstalled, " .
            "   PR.siteId, S.NAME AS siteName " .
            "FROM ho_crm_premise PR " .
            "   LEFT JOIN ho_faceplate F ON F.premiseId = PR.premiseId " .
            "   INNER JOIN bcube_site S ON S.SITEID = PR.siteId " .
            "WHERE S.SITEID";

        if( $siteId )
        {
            $sSql .= "   AND PR.siteId = " . $siteId . " ";
        }

        $pResult = $this->ExecuteSQL( $sSql );
        return $pResult;
    }


$db = new DatabaseInfinity();

$action = $_REQUEST["action"];

//$action = "SEARCH"; // TEMPORARY
$siteId = $_REQUEST["siteId"];

if( $action == "SEARCH" )
{
    $faceplates = $db->GetFaceplates( $siteId );

    while( $faceplate = FetchObject( $faceplates ) )
    {
        $tpl->newBlock( "FACEPLATE");
        $tpl->assign( "ID", $faceplate->faceplateId );
        $tpl->assign( "PREMISEID", $faceplate->premiseId  );
        $tpl->assign( "ADDRESS", $faceplate->fullAddress );
        $tpl->assign( "SITEID", $faceplate->siteId );
        $tpl->assign( "SITENAME", $faceplate->siteName );
        $tpl->assign( "FEXISTS", $faceplate->faceplateExists );
        $tpl->assign( "FINSTALED", $faceplate->faceplateInstalled );
    }
}

1 个答案:

答案 0 :(得分:0)

根据您编辑的帖子,您可以更改查询并在外部查询中包含条件faceplateExists <> faceplateInstalled以获得所需的结果,如下所示

SELECT * FROM
(
SELECT F.id AS faceplateId, PR.premiseId, 
ComposeAddressFromPAF( PR.postAddressId ) AS fullAddress, " .
            "   IF( F.id IS NOT NULL, 1, 0 ) AS faceplateExists, " .
            "   IFNULL( F.installed, 0 ) AS faceplateInstalled, " .
            "   PR.siteId, S.NAME AS siteName " .
            "FROM ho_crm_premise PR " .
            "   LEFT JOIN ho_faceplate F ON F.premiseId = PR.premiseId " .
            "   INNER JOIN bcube_site S ON S.SITEID = PR.siteId " .
            "WHERE S.SITEID
) XXX WHERE faceplateExists <> faceplateInstalled;