使用Cakephp 2.x选择id不在另一个表中的所有记录

使用Cakephp 2.x选择id不在另一个表中的所有记录,php,mysql,cakephp,cakephp-2.0,Php,Mysql,Cakephp,Cakephp 2.0,您好,我想在我的查询中实现或条件,即从另一个不存在id的表中获取所有记录或获取过去24小时内插入的记录 此查询正在获取过去24小时的结果 $this->Behaviors->attach('Containable'); return $this->find('all', array( 'contain' => array( 'User','UserInfo','QuestionAndTo

您好,我想在我的查询中实现
条件,即从另一个不存在id的表中获取所有记录
获取过去24小时内插入的记录

此查询正在获取过去24小时的结果

        $this->Behaviors->attach('Containable');
        return $this->find('all', array(

            'contain' => array(
              'User','UserInfo','QuestionAndTopic.Topic','UpVoteQuestion','Answer'

            ),
             'conditions' => array(
                'Question.created >=' => date('Y-m-d H:i:s', strtotime('-24 hour'))

            ),

            'order' => 'Question.question_id DESC',
            'recursive' => 0
        ));
我想做这样的事情

$this->Behaviors->attach('Containable');
return $this->find('all', array(

    'contain' => array(
      'User','UserInfo','QuestionAndTopic.Topic','UpVoteQuestion','Answer'

    ),
     'conditions' => array(
         'OR' => array(
    array( 'Question.created >=' => date('Y-m-d H:i:s', strtotime('-24 hour'))),
    array('Question.question_id NOT IN ' => 'Answers'),// get results where question id is not present in answers table as foreign key
)



    ),

    'order' => 'Question.question_id DESC',
    'recursive' => 0
));

我希望你能理解我的问题

有几种方法可以做到这一点

(一) 您可以使用左连接并检查没有答案的地方

在SQL中:

select * 
from questions 
left join answers on questions.id = answers.question_id
where answers.id is null;
select * from questions where id not in (select distinct question_id from answers);
在CakePHP中:

$this->find('all', array(
    'contain' => array(
        'User','UserInfo','QuestionAndTopic.Topic','UpVoteQuestion','Answer'
    ),
   'conditions' => array(
       'OR' => array(
           array( 'Question.created >=' => date('Y-m-d H:i:s', strtotime('-24 hour'))),
           array('Answer.id is null'),// get results where question id is not present in answers table as foreign key
       )
    ),
    'order' => 'Question.question_id DESC',
    'recursive' => 0
));
(二)

您可以使用子查询加载答案表中的问题ID列表,并使用基本
not in
获取补充集

在SQL中:

select * 
from questions 
left join answers on questions.id = answers.question_id
where answers.id is null;
select * from questions where id not in (select distinct question_id from answers);
在CakePHP中:

$this->find('all', array(
    'contain' => array(
        'User','UserInfo','QuestionAndTopic.Topic','UpVoteQuestion','Answer'
    ),
   'conditions' => array(
       'OR' => array(
           array( 'Question.created >=' => date('Y-m-d H:i:s', strtotime('-24 hour'))),
           array('Answer.id is null'),// get results where question id is not present in answers table as foreign key
       )
    ),
    'order' => 'Question.question_id DESC',
    'recursive' => 0
));
cakephp中的子查询有点复杂。首先必须设置子查询,然后在查询中调用该子查询。看

我从未尝试过在条件中使用子查询,但我认为这应该是可行的

$db = $this->getDataSource();
$subquery = $db->buildStatement(
    array(
        'table' => 'answers',
        'alias' => 'Answer',
        'fields' => 'question_id'
    ),
    $this
);
$this->find('all', array(
    'contain' => array(
        'User','UserInfo','QuestionAndTopic.Topic','UpVoteQuestion','Answer'
    ),
   'conditions' => array(
       'OR' => array(
           array( 'Question.created >=' => date('Y-m-d H:i:s', strtotime('-24 hour'))),
           array('Question.question_id NOT IN ' => '($subquery)'),// get results where question id is not present in answers table as foreign key
       )
    ),
    'order' => 'Question.question_id DESC',
    'recursive' => 0
));

阅读此文:@Kenyanke你好,非常感谢您分享。我已经理解了大部分内容,但我想知道的一件事是如何检查表中的数据。这里是数组('Question.Question\u id NOT IN'=>'Answers'),您的查询如何知道Question\u id存在于Answers中?@Kenyanke很抱歉,我在这方面不是很好。我只想从问题表中获取所有问题,但这些问题(问题id)不应该出现在答案表中如果您不确定,请使用query(),我希望您熟悉SQL,如果您不熟悉,我建议您学习而不是不理解代码。