比较两个表中的字段并在cakephp中获取所需的值

时间:2013-12-10 06:56:33

标签: php cakephp

select * from posts where p_id in (select distinct p_id from tabs);

$sub=$this->tab->find('all',array('fields'=>array('Distinct (tabs.p_id)')));

$sub1=$this->post->find('all',array('conditions'=>array('post.p_id' IN (' .$sub.')')));

$this->set('mov',$sub1);

任何人都可以帮助我吗

3 个答案:

答案 0 :(得分:1)

$db = $this->Post->getDataSource();
$subQuery = $db->buildStatement(
    array(
        'fields'     => array('"Tabs"."p_id"'),
        'table'      => 'tabs',
        'alias'      => 'Tabs',
        'limit'      => null,
        'offset'     => null,
        'joins'      => array(),
        'conditions' => array(),
        'order'      => null,
        'group'      => null
    ),
    $this->Post
);
$subQuery = ' DISTINCT (Tabs.p_id) IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$this->Post->find('all', compact('conditions'));

答案 1 :(得分:0)

$sub=$this->tab->find('all',array('fields'=>'Distinct tabs.p_id')));

foreach($sub as $data)
{

$postID[] = $data['Tab']['p_id'];

}

$sub1=$this->post->find('all',array('conditions'=>array('post.p_id'=>$postID)));

答案 2 :(得分:0)

试试这个

$sub1=$this->post->find('all',array('conditions'=>array('post.p_id IN ( select distinct p_id from tabs)' )));