我的 mysql 查询是这样的:
SELECT b.id, b.name, COUNT(*) AS count_store
FROM stores a
JOIN locations b ON CONVERT(b.id,CHAR) = a.address->'$."regency_id"'
GROUP BY b.id
ORDER BY count_store DESC
LIMIT 10
如果mysql查询我使用这个:CONVERT(b.id,CHAR)
将int转换为字符串
如何在laravel eloquent中将int转换为字符串?我正在使用 Laravel 5.3。
我试过这样的:
$stores = Store::select('locations.name','COUNT(*) AS count_store')
->join('locations', 'locations.id', '=', 'stores.address->regency_id')
->groupBy('locations.id')
->orderBy('count_store', 'desc')
->limit(10)
->get();
但它不起作用
如果您想使用非常自定义的连接条件,例如涉及一列或两列的强制转换,那么我不确定如果没有至少某种原始语法,这是否可行。因此,您应该考虑@Rodrane 给出的答案以及这个答案:
$stores = Store::select('locations.id', 'locations.name', DB::raw('COUNT(*) AS count_store'))
->join('locations', DB::raw("CONVERT(locations.id, CHAR)"), '=', 'stores.address->regency_id')
->groupBy('locations.id', 'locations.name')
->orderBy('count_store', 'desc')
->limit(10)
->get();
请注意,我进行了以下更改:
DB::raw()
为计数提供自定义别名location.name
到GROUP BY
(和选择)子句DB::raw()
来处理连接条件中的演员表本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句