Print Multiple Results from a Database - php

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

Related

Count duplicate columns in Laravel

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

Mysql query help to get users count from table area wise

I have a table which is having some report info. In that table I have userid and areaname .
Now my requirement is to get the count of user's list based on area
For example table will have
userid | areaname
-----------------
1 |area 1
1 |area 1
2 |area2
2 |area 2
2 |area2
3 |area1
3 |area1
4 |area3
5 |area2
---------------
Result must be
area1 2users
area2 2users
area3 1user
what is the mysql query to achieve this?
Use the Aggregate function COUNT() to get the number of users and GROUP BY to get the count based on areaname
Use DISTINCT for unique values
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname;
SELECT count(userid),area FROM YOURTABLENAME GROUP BY area;
I see, you can have dublicate entries ad well:
So I would make a sub Select
SELECT sum(partly_sum), area
FROM (
SELECT
userid, area , count(*) as partly_sum
FROM
_YOUR_TABLE_NAME_
WHERE 1
GROUP BY area, userid
) as a_bad_sub_query
WHERE 1
GROUP BY area
Regards
You can use distinct to avoid dupe entries
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname

MySQL count datas with row values without new query loop

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

Selecting a distinct value and the sum of times it shows up in a column

I have an existing table with millions of entries (growing) that consists of:
userid|name|etc...
1 frank ...
1 frank ...
2 joe ...
5 sam ...
1 franky ...
What I need to do is return a table of:
place|name|total
1 franky 3
2 sam 1
3 joe 1
Where total is the SUM(userid = the distinct userid).
Currently I'm doing a query to SELECT DISTINCT userid from table and then foreach returned value in php, I'm doing another query to return the name and sum(userid = userid).
As you can assume, this is very taxing and takes a long time now with all of the values. Is there any way to speed this up by doing 1 query?
i think you need
SELECT #a:=#a+1 AS `place`, name, COUNT(userid) AS `total`
FROM `your_table`, (SELECT #a:= 0) AS a
GROUP BY userid
SELECT userid, COUNT(*)
FROM some_table
GROUP BY userid

PHP/SQL Voting and counting up the results

I am writing a script for a car show. Users choose the car number and vote 1-5 on various criteria. There are multiple users voting for the same car, but they can only vote once for each car, I check for this.
Now I have a table of a unique carNumber and 1-5 votes for each criteria, per car.
Here is how I find the total score for each individual users vote
SELECT carNum, exterior+interior+engine AS Score FROM Judge_Votes WHERE catagory = '$catNum' ORDER BY carNum
Reults
CarNum: Score:
18 11
14 8
13 15
12 8
12 11
2 14
I want to add up the total score from each user into a final score result. IE car 12 has 19 total score.
The question I have. How can I find the total score using sql or php?
Just use SUM and ORDER BY:
SELECT carNum, SUM(exterior+interior+engine) AS Score
FROM Judge_Votes
WHERE catagory = '$catNum'
GROUP BY carNum
That should do the "trick"
SELECT carNum, sum(exterior + interior + engine) as Score
FROM Judge_Votes
WHERE gatagory = '$catNum'
GROUP BY carNum
ORDER BY carNum
The following should do the trick I think. The logic is that you group your result per car (GROUP BY carNum) and then use the build in SUM function of SQL.
SELECT SUM(exterior+interior+engine) as totalScore, carNum
FROM Judge_Votes
WHERE catagory = '$catNum'
GROUP BY carNum
ORDER BY carNum;

Categories