Background
We show a portfolio of'projects' which each have a single category, but 1 to many different services.
When the information is retrieved from the database it will populate a total of 15 different project divs which is 3 project divs for each of the 5 different categories.
While each of these projects divs has only one category, they have 1 to many different services.
Relevant Tables
Project
* projectID : smallint(5) unsigned
* businessID : smallint(5) unsigned
* categoryID : smallint(5) unsigned
* personID : smallint(5) unsigned
* reviewID : smallint(5) unsigned
* title : varchar(255)
* startDate : date
* endDate : date
* summary : varchar(255)
* img1 : varchar(255)
* img2 : varchar(255)
* img3 : varchar(255)
Service
* serviceID : smallint(5) unsigned
* serviceType : varchar(255)
* categoryID : smallint(5) unsigned
ProjectService
* projectID : smallint(5) unsigned
* serviceID : smallint(5) unsigned
The Problem
I need a query to return a table which has 15 projects, 3 from each category.. This fine and I can achieve but i also need to return the services from the projectservice table that go with each of these projects..
So far I have
(SELECT ps.*, pro.*
FROM `project` AS pro
JOIN `projectservice` AS ps
ON ps.projectID=pro.projectID
WHERE pro.categoryID = 1
ORDER BY pro.projectID DESC LIMIT 3)
UNION DISTINCT
(SELECT ps.*, pro.*
FROM `project` AS pro
JOIN `projectservice` AS ps
ON ps.projectID=pro.projectID
WHERE pro.categoryID = 2
ORDER BY pro.projectID DESC LIMIT 3)
..etc..
This correctly returns 3 projects for each category... however it will return the same project more than once for each service.
I need is;
To return 3 (and only 3) different projects (projectID) for each of the 5 different categories (categoryID)
To add the 1 or more services (serviceID) to the end of each row. So one row could be projectID=3, categoryID=3, serviceID=5 .... while the second row could be projectID=1, categoryID=3, serviceID=15, serviceID=7, serviceID=4 (note each row has a different amount of serviceID)
As far as i'm aware, '2.' is not possible as there must be a set amount of cells in each row... So what i'm after is the best query to achieve this goal...
The way I see it its either a result where rows with the same projectID and serviceID are repeated for every different serviceID recorded (in the projectservice table that matches that projectID)
or two seperate queries where the first just finds x3 distinct projectID for each of the x5 categoryID, while the second query uses those projectID to return all the records in the projectservice table that match that projectID....
or is there a better way???