gpt4 book ai didi

mysql - Kohana 中按变量排序

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

我知道这是一个愚蠢的问题,但我想知道如何使用 Kohana 正确地将变量添加到 order by ,到目前为止,我有以下查询,其中我已添加了我想要的变量对于 where 子句,这工作正常,但是当我对 order_by 应用相同的方法时,它不起作用,我是否必须以不同的方式指定它?我只需要按变量排序,因为这些变量将动态填充。

$geoquery = DB::select('store_id', 'city')
->from('mytable')
->where('latitude', '>=', $latitude * .9)
->and_where('longitude', '<=', $longitude * 1.1)
->order_by('ABS(`latitude` - 51.507202) + ABS(`longitude` - -0.223242))')
->limit(1)
->execute('mytable');

最佳答案

我想你需要使用Database Expressions为您的 order_by

$geoquery = DB::select('store_id', 'city')
->from('mytable')
->where('latitude', '>=', $latitude * .9)
->and_where('longitude', '<=', $longitude * 1.1)
->order_by(DB::Expr('ABS(`latitude` - 51.507202) + ABS(`longitude` - -0.223242))'))
->limit(1)
->execute('mytable');

根据文档

There are cases were you need a complex expression or other database functions, which you don't want the Query Builder to try and escape. In these cases, you will need to use a database expression created with DB::expr. A database expression is taken as direct input and no escaping is performed

关于mysql - Kohana 中按变量排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23426220/

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