如何将 mysql 查询转换为 laravel 5 的查询构建器?

How to convert mysql query to query builder for laravel 5?

我正在尝试从 Customers Table 中获取所有重复的行 我想将以下 mysql 查询转换为查询生成器以在 laravel 5 中使用。我在文档中搜索但不是运气。

SELECT 
   CustomerName, 
   ContactName, 
   Customers.City 
FROM 
   Customers
INNER JOIN(
            SELECT 
               City 
            FROM Customers 
            GROUP BY City 
            HAVING COUNT(CustomerName) >1 ) temp 
ON Customers.City = temp.City;

在这里我在 google 中搜索并尝试这样,但是如果 duplicate.I 只想要重复的每一行,则此查询构建器只显示一行。

> $duplicates = DB::table('customers')
->select('CustomerName','City', DB::raw('COUNT(*) as `count`'))
->groupBy('CustomerName', 'City')
->having('count', '>', 1)
->get();

如果您能提供帮助,我们将非常感谢appreciated.Thanks。

只需删除合并查询的 groupBy

Laravel 支持 whereIn.

中的子查询
$subQuery = DB::table('Customers')
    ->select('City')
    ->groupBy('City')
    ->havingRaw('count(*) > 1');

$duplicates = DB::table('Customers')
    ->select('CustomerName','City')
    ->whereIn('City', $subquery)
    ->get();

这不会创建相同的查询。但是会return同样的结果。