Reputation: 107
I have a table in my MySQL database which contains records of activity. It looks like:
Action_______________Date_________________User________________Location___________Ref Number
Action 1........1/1/2012.........J Jones.......Home.............00123
Action 2........2/1/2012.........K Smith.......Work.............00234
Action 2........2/1/2012.........J Jones.......Home.............00123
Action 1........4/1/2012.........B Smith.......Home.............00345
Action 3........5/1/2012.........J Jones.......Mobile...........00123
ETC
What I am looking for is a query that will go through the table, find the first occurrence of Action 1and then go and find Action 2 for the same Ref Number and the Action 3 etc.
It would do this for each occurrence of the Ref Number in the table.
These would need to be presented in PHP as a single data line ie:
Ref No._____________Act1date_____________Act1User_____________Act1location_____________Act2date_____________Act2User__etc
00123..........1/1/2012..........J Jones............Home....................2/1/2012..........J Jones
00234..........1/1/2012..........J Jones............Work....................2/1/2012..........J Jones 00345..........1/1/2012..........J Jones............Home....................2/1/2012..........B Smith
There will also be some calculations to be done ie time differences between Action1_date and Action2_date etc.
I can’t think how to write a query to do this – can anyone help?
Thanks in advance,
Upvotes: 0
Views: 56
Reputation: 33512
Just use an order by statement to sort the results.
select
action,
Date,
User,
Location,
refNumber
from
yourTable
order by
refNumber,
action
Upvotes: 1