Home Count duplicate columns in Laravel
Reply: 2

Count duplicate columns in Laravel

Raymond Wachaga
Raymond Wachaga Published in 2017-03-21 06:20:45Z

How do you count the DUPLICATE columns in a database table using eloquent model querying?

For example, in SQL, there's this command:

SELECT name, COUNT(email) 
FROM users
GROUP BY email 
HAVING ( COUNT(email) > 1 )

The code above will return the names from all columns with similar email addresses.

What I'm trying to achieve is to get the COUNT of duplicate entries, i.e.

$noOfjobsFromSameCustomers = App\Workflow::count();

But I need an additional parameter to specify that the cust_id (a column in the workflows table) is duplicated, something like:

$noOfjobsFromSameCustomers = App\Workflow::notDistinct()->orderBy('cust_id')->count();

For example, given the table below:

 1   John asd@asd.com 
 2   Sam  asd@asd.com 
 3   Tom  asd@asd.com 
 4   Bob  bob@asd.com 
 5   Tom  asd@asd.com 

How can I count the number of times a specified email address (e.g. asd@asd.com) recurs?

Hiren Makwana
Hiren Makwana Reply to 2017-03-21 09:12:01Z

You can use havingRow :

->havingRaw('COUNT(<columneName>) > 1')
D Coder
D Coder Reply to 2017-03-21 07:18:35Z

you can get this by raw query

$result = DB::select( DB::raw(" select * from (
  SELECT name, COUNT(email) as e_count
  FROM users
  GROUP BY email 
  )a where  a.e_count > 1 ");

and put your where condition in this query Please see for more Larave website

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298806 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO