Combine multiple rows and count into new columns in another table - mysql

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

Related

MySQL Query with three tables

I have got the following mySQL tables:
movies:
id,
title,
year
directors:
id,
name
directors_in_movies:
director_id,
movie_id
I would like to search for all the movies in which a director has been involved.
But I don't really know if I can combine three tables. I am quite new with databanks and I would be glad about some help how to write the SQL statement.
The exists operator should do the trick:
SELECT *
FROM movies m
WHERE EXISTS (SELECT *
FROM directors_in_movies dim
JOIN directors d ON dim.director_id = d.id
WHERE dim.movie_id = m.id AND
d.name = 'Some Director')

More than one value in the same row MYSQL

my problem is the next:
I have a sql query that shows information about a booking. The info is the name of the person who made the reservation, phone, etc and the extras he bought.
I need to show all the extras he bought on the same row but i cant find a method for it (im not very good at mysql)
So, i have two tables : 1 for the general info of the booking and another for the extras info because one reservation can have more than one extra and one extra could be on more than one reservation.
Table booking:
id,name,telephone
table booking_extra:
id, id_extra, id_booking
i want to
select * from booking
left join booking_extra on id_booking = booking.id
and the result should be something like
id:1
name: test
telephone: 123456
extras: 1,2,3
any ideas?
As #panther hinted in the comment, use group_concat. So you query will be like:
SELECT A.ID, A.NAME, A.TELEPHONE,
GROUP_CONCAT(DISTINCT B.ID_EXTRA ORDER BY B.ID_EXTRA ASC SEPARATOR ',') EXTRAS
FROM BOOKING A LEFT JOIN BOOKING_EXTRA B ON A.ID=B.ID_BOOKING
GROUP BY A.ID, B.ID_BOOKING;
Something like this :
SELECT id, name, telephone, GROUP_CONCAT(extras, separator ',')
FROM booking
LEFT JOIN booking_extra ON id_booking = booking.id
GROUP BY id, name, telephone

What is the SQL query to select the id's of same name?

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;

MySQL GROUP BY subject

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`

Multiple result select inside a main select?

I have a n-to-1 relation in MySQL tables.
Houses can have many owners.
I would like to list all owners for a house.
I have tried this:
SELECT id, name, address
,(
SELECT CONCAT(firstname)
FROM owners
WHERE houses_id = houses.id
) AS 'owner_firstname'
FROM houses;
but i get this results
Error Code: 1242
Subquery returns more than 1 row
I would like to have result like this
100 | Liberty House | 200, NY Street | me, myself, Iren
To address your error: you had a sub-query for your select that brought back multiple owners. The query needed to bring back one literal, in your case, the list of names. You can achieve this in MySQL using the GROUP_CONCAT() function to combine the names of owners for a particular house:
SELECT h.id, h.name, h.address, GROUP_CONCAT(o.firstname) AS owner_firstname
FROM houses AS h
INNER JOIN owners AS o ON h.id = o.houses_id
GROUP BY h.id, h.name, h.address
I think you're looking for group_concat:
select id, name, address,
(select group_concat(firstname separator ', ')
from owners where houses_id = houses.id) as 'owner_firstname'
from houses;