gpt4 book ai didi

mysql - 将 MySql 查询转换为 CakePhp 2.0

转载 作者:行者123 更新时间:2023-11-29 00:04:42 24 4
gpt4 key购买 nike

如何将此 MySql 查询转换为 CakePhp 的查找。请告诉我如何练习在 cakephp 中编写查找查询

select distinct trips.fk_userid from spots, trips 
where spots.fk_tripid = trips.id
and trips.isapproved = 1
and spots.id in (".$row[$first_index]['spot_list'].")

最佳答案

模型可以是Trip 可以这样查询

$this->Trip->query("select distinct trips.fk_userid from spots, trips where spots.fk_tripid = trips.id and trips.isapproved = 1 and spots.id in (".$row[$first_index]['spot_list'].")");

你应该创建 Trip 和 Spot 模型,在 Trip 模型中,你必须在 Spot 模型中有这个:

public $belongsTo = array(
'Trip' => array(
'className' => 'Trip',
'foreignKey' => 'fk_tripid'
)
);

然后这样查询:

$this->Spot->find('all', array(
'fields' => array("distinct Trip.fk_userid"),
'conditions' => array(
'Trip.isapproved' => 1,
'Spot.id' => $row[$first_index]['spot_list']
)
));

关于mysql - 将 MySql 查询转换为 CakePhp 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28087636/

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