Inner join limit
744152May 20 2010 — edited May 20 2010Hi all,
I have two tables as such
Event (id,name)
0, event1
1, event2
2, event3
and
Step (id, event, name, order, complete)
0,0, step1, 1, false
1,0, step2, 2, false
2,1, step1, 1, true
3,1, step2, 3, false
4,1, step3, 2, false
5,2, step1, 1, true
Basically I am trying to create a query that returns the following result
Step (id, event, name, order, complete)
0,0, step1, 1, false
4,1, step2, 2, false
The more observant out there will have noticed that the result set is a list of the next incomplete step for each event.
My problem is that I'm not too sure about the SQL involved. Here is what I have so far
SELECT S.* FROM EVENT E
INNER JOIN STEP ON S.EVENT = E.ID
WHERE S.COMPLETE = 0
ORDER BY S.ORDER ASC
This would work fine except that I get all the next incomplete steps, i.e. I get step2 and step3 returned for event2 where as I only want step2.
Is there a way to limit the inner join to just one?
Moreover, am I going about this the right way?
Any help would be much appreciated.
Thanks.