如何在laravel eloquent中将int转换为字符串?

塞缪尔·托

我的 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.nameGROUP BY(和选择)子句
  • 使用另一个DB::raw()来处理连接条件中的演员表

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C中将int转换为字符串?

如何在bash中将字符串转换为int?

如何在C ++中将字符串转换为int?

如何在PostgreSQL中将int转换为字符串

如何在wpf中将int转换为字符串?

如何在inloop中将int转换为字符串

如何在python中将字符串转换为int?

如何在mongodb中将字符串转换为int

如何在Laravel中将数组转换为字符串?

如何在laravel中将数组转换为字符串

如何在python中将unsigned char类型的int字符串转换为int

如何在 Arduino 中将字符串转换为字符 *?

如何在C中将`int`数组转换为IP地址字符串?

如何在Java中将字符串转换为int数组时抛出异常?

如何在 CLASSIC ASP 中将字符串变量转换为 int,反之亦然

如何在PowerShell中将字符串值转换为键入“ System.Int32”

我如何在 Java 中将字符串 '$1400.00' 转换为 int 1400.00

如何在Linux内核中将char []字符串转换为int?

如何在Golang中将数字(int或float64)转换为字符串

如何在Go中将int值转换为字符串?

如何在C#中将unicode字符串转换为int?

如何在Kotlin中将Int转换为十六进制字符串?

如何在Swift中将Int转换为十六进制字符串

如何在C中将int转换为字符串(char *)

如何在scala中将字符串数组转换为int数组

如何在csv文件中将字符串转换为int?

如何在存储过程中将字符串转换为int

如何在JS中将64位数字字符串转换为int

如何在 Swift 中将包含 % 的字符串转换为 Int