I have a table called 'teachers'.
teachers table has the following fields
id
schoolnumber
gender
firstname
lastname
subjectgroup
status
time
I would like to display all the teachers based on their subject (subjectgroup).
Here is my current query:
SELECT * FROM teachers WHERE status = '1' GROUP BY subjectgroup
It is grouping the teachers correctly but it's only showing 1 teacher per subject.
Output:
Science:
Mr. ScienceMan
English:
Mrs. English
Math:
Mr. Math
but i want is the listing of teachers by subject
There should be more than one teacher in the groups.
Thanks for your help!
Group by will group those rows into one row. It sounds like you want to order by subject instead
For what you're doing, you want ORDER BY, which sorts by the given field.
GROUP BY is for times when you're trying to get summary data, as when you've got a SUM() or AVERAGE() or maybe a COUNT() in the SELECT statement. It tells SQL how to break up which items to put together for those functions.
I would suggest something like this:
SELECT DISTINCT subjectgroup, GROUP_CONCAT(firstname SEPARATOR ' ')
FROM teachers
WHERE status = 1
GROUP BY subjectgroup;
Group concat is the function you need in MySQL
This will bring a list of the teacher names separated by commas:
SELECT
GROUP_CONCAT(
DISTINCT (
CONCAT_WS(' ', `firstname`, `lastname`)
) SEPARATOR ', '
) AS `teacher_list`,
`subjectgroup`
FROM
`teachers`
WHERE
`status` = 'i'
GROUP BY
`subjectgroup`
Related
I need help with query how to fetch who voted and how many votes were given to each country using MySQL?
So, I have table voter and result, you can find the dummy dataset here https://github.com/FirzaCank/Project/blob/main/SQL/Self%20Project/Vote%20Poll/Dataset%20Dummy%20voter.sql
Which voter table contains:
ID (INT)
first_name VARCHAR(10)
last_name VARCHAR(10)
and result table contains:
voter_id (INT)
country VARCHAR(10)
I've tried a MySQL query like this:
SELECT
country,
CONCAT(v.first_name,' ',v.last_name,' x ',COUNT(r.voter_id)) AS votes
FROM voter v
RIGHT JOIN result r ON v.id = r.voter_id
GROUP BY country;
But I got an error, I'm sure this problem need something like loops but I don't really understand that stuff.
The desired output is will be like this, but as far as I've tried in the above syntax it just came out with my output which I can't display all voter in the same country, I just came out with 1 voter every 1 country.
SELECT
country,
group_concat(votes,',') as votes
FROM
(
SELECT
country,
CONCAT(v.first_name,' ',v.last_name,' x ',COUNT(r.voter_id)) AS votes
FROM voter v
RIGHT JOIN result r ON v.id = r.voter_id
GROUP BY country,first_name,last_name
) x
GROUP BY country;
see: DBFIDDLE
First your query give an error. This one:
"SELECT list is not in GROUP BY clause and contains nonaggregated
column '...first_name' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by"
This error, and how to handle it, is epxlained here: MySQL Handling of GROUP BY
This means that all fields that are not used in an aggregate function, you be mentioned in the GROUP BY.
Because your desire is that the output only contains 1 line per country the GROUP_CONCAT function is added on the result.
Another way, (probably a bit more optimal on big data, because aggregation happens over single table):
select country, group_concat(concat(first_name, ' ', last_name, ' x ', c) order by c desc)
from (
select country, voter_id, count(*) c
from result
group by country, voter_id
) x
join voter on id = voter_id
group by country;
create list of all writer and the count of books they have written .
*Create one new column as writers name adding the first and last name from the writers table
*Get the count of books they have written in a new coulmns as number of books written
*Finally List to be ordered by Writers name , title and launch date
Im have created a simple question here as im trying to build something on similar ground:
Books:
Writers:
Expected Result:
SELECT *
FROM books b
INNER JOIN (
SELECT CONCAT(first_name,' ', last_name) AS author,COUNT(*) As booksWritten
FROM books
GROUP BY CONCAT(first_name,' ', last_name) c
ON CONCAT(b.first_name,' ', b.last_name=c.author
Is this what you want?
In MySQL 8.x you can use a window function. For example:
select
b.book_id,
b.title,
b.launch_date,
concat(w.first_name, ' ', w.last_name) as writers_name,
count(*) over(partition by w.first_name, w.last_name) as book_count
from book_table b
join write_table w on w.book_id = b.book_id
order by b.launch_date
I have a table like this:
Basically, I want to group all names and give the count of such groupings like:
Since there are 2 users with cooking and cleaning, 1 for washing and 4 for cooking.
I am trying to use group_concat
SELECT group_concat(DISTINCT name) AS name, count(*)
FROM warehouse.test1
GROUP BY guid
However, this will not work as I need to group by the groupings and then get count of those groupings.
I am not getting how to group on the groupings and then get the count.
Thanks in advance and I appreciate any heads up!
UPDATE
As per the answer I tried
SELECT groupings, COUNT(*)
FROM (SELECT group_concat(DISTINCT name) AS groupings
FROM warehouse.test1
GROUP BY saguid) t
GROUP BY groupings;
However, I get it as
# groupings, COUNT(*)
'cleaning,cooking', '2'
'cooking', '2'
'washing', '1'
shoudnt count be 4 for cooking?
I'd wrap this query with another query to coubt the grouped data:
SELECT groupings, COUNT(*)
FROM (SELECT group_concat(DISTINCT name) AS groupings
FROM warehouse.test1
GROUP BY saguid) t
GROUP BY groupings
I want to combine two or more rows which have the same name and put their values in two separate column. The problem can be clearer with the following images:
My expected output is:
You can check the fiddle here: fiddle
What I have tried so far is with the MySQL code:
Select subjects, mark_score, activity
FROM(
SELECT subjects, mark_score,
(SELECT regd, subjects, mark_score
FROM exo_i WHERE entry='7' and regd='19') as activity
FROM exo_i WHERE regd='19' GROUP BY subjects)t
As discussed in the comment, the requirement is to display "marks" for the "FA1" entry and "activity" for the "SA1" activity. Assuming there can't be multiple rows with these values (i.e., the combination of regd, subjectsa and activity is unique), you could have a subquery for each of these activities, and join them:
SELECT a.regd, a.subjects, a.marks, b.activity
FROM (SELECT regd, subjects, marks
FROM mytable
WHERE entry = 'FA1') a
JOIN (SELECT regd, subjects, marks AS activity
FROM mytable
WHERE entry = 'SA1') b ON a.regd = b.regd AND a.subjects = b.subjects
This is my table:
Table Name:`sample`
id|name
1|rama
2|seetha
3|rama
4|seetha
5|rama
6|seetha
7|rama
8|seetha
I need to display the unique name and id.
The following is the query I have used to get the name and ids:
"SELECT sample.id, sample.name FROM sample group by name"
Two names are displaying but I need the different ids used for that name.
Maybe you want to look in to GROUP functions. Something like this should do it:
SELECT GROUP_CONCAT(sample.id) AS ids, sample.name FROM sample group by name;
Try use GROUP BY to group all records by name and GROUP_CONCAT function to join the ids for the name:
SELECT s.name, GROUP_CONCAT(DISTINCT s.id ORDER BY s.id ASC SEPARATOR ', ') AS ids FROM `sample` s GROUP BY s.name;