gpt4 book ai didi

php - CakePHP:如何使用内部联接从两个表中检索数据?

转载 作者:行者123 更新时间:2023-11-29 01:28:58 24 4
gpt4 key购买 nike

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

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

我在 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')
)
)
)
)));

但是会产生这个错误:

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

可能是什么问题?

最佳答案

我认为你的语法是错误的,因为选项数组应该有一个用于连接的键。您似乎有一个额外的数组。尝试:

$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')
))
)
));

关于php - CakePHP:如何使用内部联接从两个表中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23981352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com