Sou*_*der 6 php sql database laravel laravel-5
我想从用户表中获取所有相同名称和位置的行
**id** |name |location |phone_number
1 |John | Europe |0988884434
2 |john | Europe |0933333333
3 |Michael |Europe |0888888888
4 |Smith |Dubai |082388888888
5 |Smith |Dubai | 03939494944
Run Code Online (Sandbox Code Playgroud)
我想获取所有具有相同名称和位置的行
john |Europe
john |Europe
Smith |Dubai
Smith |Dubai
Run Code Online (Sandbox Code Playgroud)
这是我尝试做的
$duplicates = DB::table('users')
->select('name','location', DB::raw('COUNT(*) as `count`'))
->groupBy('name', 'location')
->having('count', '>', 1)
->get();
Run Code Online (Sandbox Code Playgroud)
但这只是显示只有一行是重复的
john |Europe
Smith|Dubai
Run Code Online (Sandbox Code Playgroud)
您的任何帮助或建议将不胜感激。
Tim*_*sen 13
使用havingRaw:
$duplicates = DB::table('users')
->select('name','location', DB::raw('COUNT(*) as `count`'))
->groupBy('name', 'location')
->havingRaw('COUNT(*) > 1')
->get();
Run Code Online (Sandbox Code Playgroud)
我也不确定语法,但Laravel 文档似乎暗示您在 select 子句中定义的别名在普通having()函数中不可用。
| 归档时间: |
|
| 查看次数: |
8249 次 |
| 最近记录: |