下面的 cakephp 行从我的数据库中返回最高的 3 car_ids
$cars = $this->car->find('all',array('limit' => 3, 'order' => array('car.id' => 'desc')));
我想使用以下值具体说明我需要拉哪 3 辆车:
car_id={3,7,10};
正确的命令是什么?
您需要
在查询中使用IN
条件。
在MySQL中,这看起来像:-
WHERE car.id IN (3, 7, 10)
在CakePHP 2.x中,你可以这样做:-
$cars = $this->car->find('all', array(
'conditions' => array(
'car.id' => array(3, 7, 10) // This is the `IN` condition
),
'limit' => 3,
'order' => array('car.id' => 'desc')
));
CakePHP 会自动推断你想使用 IN
因为条件中的值是一个数组。
在 CakePHP 3.x 中,我们需要告诉 CakePHP 使用IN
,以便它将条件值转换为数组(否则,如果条件值不是 Cake 所期望的,您可能会收到错误(:
$cars = $this->car->find()
->where(['id IN' => [3, 7, 10]]);
->limit(3)
->order(['id' => 'DESC']);
您只需要使用 find 方法,然后使用一些条件来获取所需行的值。
我在您的car_id中使用OR,因为我认为您想获取所有具有car_id
的行。
您更新的查询:
$cars = $this->car->find()
->where(['OR' => [['car_id' => 3], ['car_id' => 7], ['car_id' => 10]]])
->limit(3)->order(['car_id' => 'DESC']);
我想你可能会得到帮助。有关CakePHP Query的更多信息,请参阅更多
试试这个查找查询它为你工作....其返回数据car_id 3,7,10。
$cars = $this->Car->find('all',array("conditions"=>array("car_id IN"=> [3,7,10] ),"order"=>"Car.car_id DESC"));
试试这个
car.id 或car_id无论您的字段名称是什么 id
$car = $this->car->query('select * from tablename where car.id IN ("3","7","10")');