简体   繁体   English

与Zend_Validate_Db_RecordExists的联合查询

[英]Union Query with Zend_Validate_Db_RecordExists

I need to check if a record exists or not in php zend. 我需要检查php zend中是否存在记录。

I have the following sql query which works fine and gives the proper results 我有下面的sql查询,可以正常工作,并给出正确的结果

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

I want to write this query using Zend_Validate_Db_RecordExists to check if record with email= 'demo@xyz.com' exists or not under said conditions. 我想使用Zend_Validate_Db_RecordExists编写此查询,以检查在上述条件下email= 'demo@xyz.com'存在带有email= 'demo@xyz.com'记录。

I don't know why you would use a UNION for this, the following code should do the job: 我不知道为什么要为此使用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";
    }
}

The code excludes records that either have isLogged != 1 or idPeople != 5 , please let me know if I misunderstood. 该代码不包括具有isLogged != 1idPeople != 5 ,如果我误解了,请告诉我。

Edit: I got curious about UNION vs OR performance and found this link explaining some of the details: https://stackoverflow.com/a/13866221/3784145 编辑:我对UNION vs OR性能感到好奇,发现此链接解释了一些详细信息: https : //stackoverflow.com/a/13866221/3784145

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM