[英]CakePHP: How to retrieve data from two tables using an inner join?
我在數據庫中有兩個表,一個作為user(id,first_name,last_name)
,另一個作為location(id,country)
。
我需要根據條件user.id = location.id
對這兩個表執行內部user.id = location.id
,並且查詢結果應包含列first_name
, last_name
和country
。
我在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')
)
)
)
)));
但是會產生此錯誤:
“字段列表”中的未知列“ location.country”
可能是什么問題呢?
我認為您的語法是錯誤的,因為options數組應具有用於聯接的鍵。 您似乎有一個額外的array
。 嘗試:
$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')
))
)
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.