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:
ID NAME EMAIL
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?
You can use havingRow :
->havingRaw('COUNT(<columneName>) > 1')
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
Related
Hi i am trying to get a mysql query working and need some help. I have 2 tables (staff, booking).
Staff table contains staff details
StaffID
First_name
Last_name
1
John
Doe
2
Mary
Doe
Booking table contains
BookingID
StaffID
Status
1
1
cancelled
2
1
cancelled
3
1
confirmed
4
2
cancelled
5
1
confirmed
I would like to get the count of confirmed/cancelled bookings for each staff but is having trouble getting it to work. My current query that I'm using is
Select staff.StaffID, staff.First_name, staff.Last_name, sum(booking.Status LIKE '$status') as Status
from staff, booking
where staff.StaffID = booking.ConvenerID
group by staff.StaffID
$status being cancelled or confirmed. With this query I'm only able to display a row if there is a status value, if a staff have no cancelled it does not display the row for that staff. How can I get the row to display even if the count of $status is 0.
You need a LEFT join of the tables:
SELECT s.StaffID, s.First_name, s.Last_name,
COALESCE(SUM(b.Status = ?), 0) AS counter
FROM staff s LEFT JOIN booking b
ON s.StaffID = b.StaffID
GROUP BY s.StaffID;
Replace ? with the status value that you want (there is no reason to use the operator LIKE because a simple = works fine).
I've 4 table for a newsletter. Newsletters, Subscribers, Subscriber Groups and Selected Subscriber Groups. I've choose subscriber groups in campaign edit area, and its save selected groups to tbl_newsletter_groups table like;
tbl_newsletters
NID title details
1 text 1 content 1
2 text 2 content 2
tbl_subscriber_groups
GID group_name
5 group 1
6 group 2
tbl_subscribers
SID GID email name
10 5 sub1#mail.com sub1 name
11 6 sub1#mail.com sub1 name
tbl_newsletter_groups
NGID NID GID
15 1 6
16 1 6
17 1 6
I want to show total selected subscriber count when I list newsletters in my page. My soulution works fine, Im looking for simple and clearly statement, there any faster way available like in single newsletter list statement?
Here my own count style (yes I know its too bad and long way);
$subGID = array();
$list = $myconn->query("SELECT * FROM tbl_newsletters");
while($listRs = $list->fetch_assoc()){
$grps = $myconn->query("SELECT * FROM tbl_newsletter_groups WHERE NID=". $listRs['NID'] ."");
while($grpsRs = $grps->fetch_asscoc()){
$subGID[] = $grpsRs['GID'];
} $grps->free();
$subs = implode(" OR GID=",$subGID);
$count = mysqli_num_rows($myconn->query("SELECT ID FROM tbl_subscribers WHERE GID=". $subs));
echo('Total Selected Subscriber: '.$count);
} $list->free();
Thanks.
The search term you want is "set-based logic".
Your thinking is sound: you need everything from tbl_newsletters, then you need to count results from tbl_subscribers, but in order to get those you need information from tbl_newsletter_groups.
In SQL, that's an indication you want a join. You've already discovered the conditions you need, you just don't know the syntax. A reference manual can help there.
Now you'll have a bunch of records, which you need to smash into a smaller number of records. You need aggregation functions and a GROUP BY clause.
So here's the final query:
SELECT n.NID, n.title, n.details, COUNT(s.SID)
FROM tbl_newsletters AS n
JOIN tbl_newsletter_groups AS g ON n.NID = g.NID
JOIN tbl_subscribers AS s ON g.GID = s.GID
GROUP BY n.NID
I have table like this one
id name
1 John
2 Mike
3 Zed
4 Teacher
5 Aaron
...........
How to make mysql query to get rows in ASC order by name but put "Teacher" at the top or something that starts with "Teacher"?
select * from your_table
order by case when name = 'Teacher' then 1 else 2 end,
name
and for MySQL the following works since it returns 0 for false and 1 for true
select * from your_table
order by name <> 'Teacher',
name
Use FIELD function
SELECT * FROM table ORDER BY FIELD(`name`,'Teacher') ASC
select * from table order by case when name like "Teacher%" then 1 else 2 end,name
Names starting with Teacher will be on top sorted alphabetically, then the rest.
Example
Teacher
Teacher Aaron
Teacher Zed
Aaron
Zed
I have a Database table in MYSQL, it looks like this:
Project_ID user_ID name
11 1 fred
11 2 rick
11 1 fred
I want to get the names in the table, but when I display it I get the same name twice because the user_ID 1 appears twice in the table. I tried to display it with GROUP BY and SUM, but I don't want to do it this way. How do I get it not to return duplicate rows?
Use DISTINCT
SELECT DISTINCT user_ID
, verschil_ma
FROM project_uren
WHERE project_ID = 11
GROUP BY user_ID
Point 1 is that should the user be assigned to the project twice?
Point 2 - Use the DISTINCT keyword to return only unique records - http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
SELECT DISTINCT user_ID
FROM TABLE
WHERE Project_id = 11
That will return you 1 and 2 (You won't get 1 twice)
Thanks
$results = // query
$results = array_unique($results);
Currently I have an sql query which finds the amount of records in the database for a specific person (Ms White), how can I repeat the function for every surname within the table and print them out in a sensible format?
$query = "SELECT COUNT(Surname) FROM Customers WHERE Surname='White'";
GROUP BY I think is what you are looking for
SELECT COUNT(*),Surname FROM Customers GROUP BY Surname
Wouldn't it be better to run just one query with the count per each surname?
select Surname, count(*) as Total from Customers
group by Surname
This will return results like this:
Person Total
White 4
Mustard 2
Plum 1
etc...
Use
SELECT count(1) as "Counter", surname FROM Customers GROUP BY surname
This will give you output like below.
Counter + Surname
++++++++++++++++++++
10 + SN1
15 + SN2
11 + SN3