relational schema:
contact {contact_id, First_Name, Last_Name}
Experience {contact_id, credit, startdate, enddate}
This is a movie credit database. Say the report is for Producers, the application needs to return a report with the following columns:
Name - Months as Producer - Most recent credit
Billy Joe - 120 - Executive Producer
Ann Yip - 60 - Producer
Jelly Morton - 24 - Director
The problem is this report must be generated by only one query. It could be generated by two queries, but the following is not fast enough:
SELECT First_Name, Last_Name SUM(PERIOD_DIFF(DATE_FORMAT(enddate,"%y%m")+1, DATE_FORMAT(startdate,"%y%m"))) as months FROM contact, Experience WHERE contact.contact_id=Experience.contact_id and credit="Producer" GROUP BY contact.contact_id ORDER BY months desc;
Followed by a query on each result which said
SELECT credit from Experience WHERE contact_id=$row[contact_id] ORDER BY startdate DESC;
However, because of the number of records which must be returned at once, this method is too slow. The result set must be returned by one query.
Does anyone know the solution to this problem?