Select ID 不在另一个 table 中的所有记录使用 Cakephp 2.x
Select all records where the id is not in another table using Cakephp 2.x
你好我想在我的查询中实现 OR
条件,它将从另一个 table 中获取所有记录,其中 id 不存在 OR
获取已插入的记录最后 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
));
希望你能理解我的问题
有几种方法可以做到这一点。
1)
您可以使用左连接并检查没有答案的地方。
在SQL中:
select *
from questions
left join answers on questions.id = answers.question_id
where answers.id is null;
在 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
));
2)
您可以使用子查询加载答案中的问题 ID 列表 table,并使用基本 not in
获取补充集。
在SQL中:
select * from questions where id not in (select distinct question_id from answers);
在 CakePHP 中:
cakephp 中的子查询有点复杂。您首先必须设置子查询,然后在查询中调用它。参见 https://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries
我从未尝试在条件中使用子查询,但我认为这应该可行。
$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
));
你好我想在我的查询中实现 OR
条件,它将从另一个 table 中获取所有记录,其中 id 不存在 OR
获取已插入的记录最后 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
));
希望你能理解我的问题
有几种方法可以做到这一点。
1) 您可以使用左连接并检查没有答案的地方。
在SQL中:
select *
from questions
left join answers on questions.id = answers.question_id
where answers.id is null;
在 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
));
2)
您可以使用子查询加载答案中的问题 ID 列表 table,并使用基本 not in
获取补充集。
在SQL中:
select * from questions where id not in (select distinct question_id from answers);
在 CakePHP 中:
cakephp 中的子查询有点复杂。您首先必须设置子查询,然后在查询中调用它。参见 https://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries
我从未尝试在条件中使用子查询,但我认为这应该可行。
$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
));