我需要检查 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 != 1
或 idPeople != 5
的记录,如果我理解错误,请告诉我。
编辑:我对 UNION 与 OR 性能感到好奇,并发现此链接解释了一些细节:https://stackoverflow.com/a/13866221/3784145
关于php - 使用 Zend_Validate_Db_RecordExists 进行联合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000726/