I've been trying to write a mySQL-statement for the scenario below, but I just can't get it to work as intended. I would be very grateful if you guys could help me get it right!
I have two tables in a mySQL-database, event and route:
event:
id | date | destination | drivers | passengers | description | executed
route:
name | distance
- drivers contains a string with the usernames of the registered drivers in an event on the form "jack:jill:john".
- destination contains the event destination (oh, really?) and its value is always the same as one of the values in the field name in the table route (i.e. the destination must already exist in route).
- executed tells if the event is upcoming (0) or already executed (1).
- distance is the distance to the destination in km from the home location.
What I want is to get the total distance covered for one specific user, only counting already executed events.
E.g., if Jill has been registered as a driver in two executed events where the distances to the destinations are 50km and 100km respectively, I would like the query to return the value 150.
I know I can use something like ...WHERE drivers LIKE '%jill%' AND executed = 1
to get the executed events where Jill was driving, and SUM()
to get the total distance, but how do I combine the two tables and get it all to work?
Your help is very much appreciated!
/Linus