I have a database full of sports results. I would like to select some results based on some characteristics of previous results. Here is the database structure:
CREATE TABLE `results` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `date` DATETIME NOT NULL , `home_score` INT NOT NULL , `away_score` INT NOT NULL , `home_team_id` INT NOT NULL , `away_team_id` INT NOT NULL );
So, I would like to make queries such as โfind results when the team won two of their previous home gamesโ, that is, for a certain order home_team_id by date, and then select each row, where in the previous two lines home_score> away_score,
I know this is a bit complicated, so any pointers on how to solve this problem will be widely appreciated. I currently have a version in PHP (selects all rows and then executes this type of query), but the performance is very slow and it uses a huge amount of memory (the database contains more than 20,000 rows).
EDIT: Thanks for a couple of neat answers. Ideally, however, I would like to be able to run queries across all columns, rather than just looking at W, D or L. A more difficult example would be to โfind all the results when the home team won each of their five previous game houses, at least at least two goals, and the away team lost at least one goal to each of their away games. "
source share