dpxo13079
2014-06-01 15:22
浏览 111
已采纳

CakePHP:如何使用内连接从两个表中检索数据?

I have two tables in a database, one as user(id,first_name,last_name), and the other one as location(id,country).

I need to perform an inner join with these two tables based on the condition user.id = location.id, and the query result should contain the columns first_name, last_name and country.

I have tried the following query in CakePHP:

$this->set('users', $this->User->find('list', array(
    'fields' => array(
        'User.id',
        'User.first_name',
        'location.country'
    ),
    array(
        'joins' => array(
            array(
                'table' => 'location',
                'alias' => 'location',
                'type' => 'INNER',
                'conditions' => array('User.id = location.id')
            )
        )
    )
)));

which however produces this error:

Unknown column 'location.country' in 'field list'

What could be the problem?

图片转代码服务由CSDN问答提供 功能建议

我在数据库中有两个表,一个作为 user(id,first_name,last_name),另一个作为 location(id,country)

我需要根据条件对这两个表执行内连接 user.id = location.id ,查询结果应包含 first_name last_name country

我在CakePHP中尝试了以下查询:

  $ this-> set('users',$ this-> User-&gt  ; find('list',array(
'fields'=> array(
'Andage.id',
'User.first_name',
'location.country'
),
 array(  
'joins'=>数组(
数组(
'表'=>'位置',
'别名'=>'位置',
'类型'=>'INNER',  
'conditions'=> array('User.id = location.id')
)
)
)
))); 
   
 
 <  p> whic 然而,h会产生此错误: 
 
 

'字段列表'中的未知列'location.country'

可能是什么问题?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dt1888 2014-06-01 15:59
    最佳回答

    I think your syntax is wrong because the options array should have a key for the joins. You appear to have an extra array. Try:

    $this->set('users',$this->User->find('list', 
      array(
           'fields' => array('User.id', 'User.first_name','location.country'),
           'joins' => array(array('table' => 'location',
                                   'alias' => 'location',
                                   'type' => 'INNER',
                                   'conditions' => array('User.id = location.id')
                             ))
             )
      ));
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题