如何在Laravel 5.4中返回所有具有相同列的重复行?

3

我想在求职者表格中获取所有同名和国家的求职者列表。

**id** |fullname    |country    |phone_number

1      |John        | singapore |0988884434

2      |john        | singapore |0933333333

3      |Michael     |Malaysia   |0888888888

4      |Smith       |Dubai      |082388888888

5      |Smith       |Dubai      |03939494944

what am I expect here is,

john  |singapore
john  |singapore
Smith |Dubai
Smith |Dubai

我在这里尝试的是:

 $duplicates = DB::table('jobseekers')
                ->select(array('jobseekers.fullname','jobseekers.country', DB::raw('COUNT('*')'))
                ->groupBy('jobseekers.fullname','jobseekers.country')
            ->having(DB::raw('COUNT('*')'))
                ->get();
       var_dump($duplicates); 

非常感谢您提供的任何帮助或建议。

2个回答

7
尝试这个:
$duplicates = DB::table('jobseekers')
    ->select('fullname','country', DB::raw('COUNT(*) as `count`'))
    ->groupBy('fullname', 'country')
    ->having('count', '>', 1)
    ->get();

先生,显示如下: (2/2) QueryException SQLSTATE[42S22]: 列未找到:1054 在字段列表中未知的列'country'(SQL: select fullname, country, 0 from jobseekers group by fullname, country having count > 1) - undefined
@ThanHtutOo 我不知道为什么 country 是一个"未知列",但我刚刚修复了你代码中的另一个错误(COUNT('*') 应该是 COUNT(*))。 - undefined

1

您需要使用INNER查询来获取行,例如:

SELECT fullname, country
FROM jobseekers
WHERE fullname IN (
    SELECT fullname
    FROM jobseekers
    GROUP BY fullname
    HAVING COUNT(*) > 1
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接