-1

I need to select 8 people from a movie crew. I only want to have 3 producers and then let the query continue with other jobs.

This is my query now:

SELECT EmployeeID, j.NameEN, e.Firstname, e.Lastname, j.ID AS JobId 
  FROM crew AS c
  LEFT JOIN job AS j ON c.JobID = j.ID
  LEFT JOIN employee AS e ON c.EmployeeID = e.Id
  WHERE c.MovieID = 237038
  ORDER BY j.SortOrder ASC, c.JobID ASC, e.Score DESC
  LIMIT 8

How can I achieve selecting only 3 employee's with the same job id?

Sample output

EmployeeID NameEN       Firstname     Lastname   JobId 
802        Director     Chris         Columbus   2
5707       Director     David         Yates      2
1705       Director     Mike          Newell     2
175        Director     Alfonso       Cuarón     2
5115       Writer       Steve         Kloves     3
2041       Writer       Michael       Goldenberg 3
11096      Writer       J.K.          Rowling    3

You can see here that I have 4 Directors. The desired output would be that there were 3 Directors max.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Petah
  • 127
  • 1
  • 7

1 Answers1

1

I think the simplest method for your particular problem is union all:

(SELECT EmployeeID, j.NameEN, e.Firstname, e.Lastname, j.ID AS JobId 
 FROM crew c LFT JOIN
      job j
      ON c.JobID = j.ID LEFT JOIN
      employee e
      ON c.EmployeeID = e.Id
 WHERE c.MovieID = 237038 AND
       j.NameEN = 'Producer'
 ORDER BY j.SortOrder ASC, c.JobID ASC, e.Score DESC
 LIMIT 3
) UNION ALL
(SELECT EmployeeID, j.NameEN, e.Firstname, e.Lastname, j.ID AS JobId 
 FROM crew c LFT JOIN
      job j
      ON c.JobID = j.ID LEFT JOIN
      employee e
      ON c.EmployeeID = e.Id
 WHERE c.MovieID = 237038 AND
       j.NameEN <> 'Producer'
 ORDER BY j.SortOrder ASC, c.JobID ASC, e.Score DESC
 LIMIT 5
);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786