There are 2 tables:
CREATE TABLE movie ( movieId INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, description VARCHAR(255) NULL, active BOOLEAN, PRIMARY KEY (movieId) ); CREATE TABLE seance ( seanceid INT NOT NULL AUTO_INCREMENT, date DATE NOT NULL, cost INT NOT NULL, sold INT NOT NULL, active BOOLEAN, movieId INT NOT NULL, PRIMARY KEY (seanceId) ); The first table stores only movies. In the second session (many sessions for each film), cost - the cost of a ticket, sold - the number of tickets sold per session. Movie.active - show or removed from hire. Seance.active - canceled or not.
You need to make a sample: movieId, m.name, m.active, the amount of money earned by each movie, at all sessions (do not take into account money from canceled sessions), whether there will be shows with the participation of this movie today and in the future.
Here is what I get at the moment:
SELECT m.movieId, m.name, m.active, IFNULL(SUM(s.sold*s.cost), 0) AS earned, EXISTS(SELECT s.date >= getDate()) as work FROM movie AS m LEFT JOIN seance AS s ON (m.movieId = s.movieId) WHERE s.active GROUP BY m.movieId But it doesn’t work out with work. Help make a selection and point out errors. Thank you in advance!