Ad
Sql Query COUNT Number Of Occurences When Joining A Table
Im trying to return the names of all members that have registered a tasting of a coffe in my DB, and count number of tasting each name has left.
select bruker.navn, COUNT(*) from kaffesmaking inner join bruker on kaffesmaking.bruker_epost = bruker.epost
table "user" has the columns
name, email , password
table "tasting (alias kaffesmaking) has the columns:
id, note, date, score, user_email, coffename
I want a list displaying
names with numbers of tastings done by that particular user.
i.e
John Johnson 5 Jessie ashford 3 Paul newman 8
Where the number represents the number of rows that specific user has added in the tasting table.
Any help would be appreciated
Ad
Answer
You were almost there, you need to add a "group by" to your SQL
SELECT bruker.navn
, COUNT(*) as tastings
FROM kaffesmaking
INNER JOIN bruker
ON kaffesmaking.bruker_epost = bruker.epost;
group by bruker.navn;
Ad
source: stackoverflow.com
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key
Ad