联合查询 Zend_Validate_Db_RecordExists

Union Query with Zend_Validate_Db_RecordExists

我需要检查 php zend 中是否存在记录。

我有以下 sql 查询,它工作正常并给出了正确的结果

(SELECT * FROM email where isLogged = 1 and emailId='demo@xyz.com') 
UNION 
(SELECT * FROM email where idPeople = 5 and emailId='demo@xyz.com');

我想使用 Zend_Validate_Db_RecordExists 编写此查询,以检查在所述条件下是否存在带有 email= 'demo@xyz.com' 的记录。

我不知道你为什么要为此使用 UNION,下面的代码应该可以完成这项工作:

$email     = 'demo@xyz.com';
$clause    = $dbAdapter->quoteIdentifier('isLogged') . ' != 1 OR ' . $dbAdapter->quoteIdentifier('idPeople') . ' != 5';
$validator = new Zend\Validator\Db\RecordExists(
    array(
        'table'   => 'email',
        'field'   => 'emailId',
        'adapter' => $dbAdapter,
        'exclude' => $clause
    )
);

if ($validator->isValid($email)) {
    // username appears to be valid
} else {
    // username is invalid; print the reason
    $messages = $validator->getMessages();
    foreach ($messages as $message) {
        echo "$message\n";
    }
}

该代码排除了 isLogged != 1idPeople != 5 的记录,如果我理解有误,请告诉我。

编辑: 我对 UNION 与 OR 的性能很好奇,发现这个 link 解释了一些细节: