I am currently using this SQL in a PHP app and need to convert to MS Access.
I need to run the second query against each result from the first to end up with a count of riders in each race the rider in the first race ran. (motorcycle racing events)
Quite simple with PHP (I can provide the complete code for that if needed)
Not real sure how to even begin with Access. (both query's work fine in Access, just not able to join them how I need to)
Query 1:
SELECT schedule2.Date, riders.ridername, results.finish, results.points, classes.class, seasons.season
FROM schedule2 RIGHT JOIN (classes RIGHT JOIN ((riders RIGHT JOIN results ON riders.id = results.rider_id) LEFT JOIN seasons ON results.season_id = seasons.id) ON classes.id = results.class_id) ON schedule2.id = results.schedule_id
GROUP BY schedule2.Date, riders.ridername, results.finish, results.points, classes.class, seasons.season, schedule2.id, results.class_id, riders.id
HAVING (((riders.id)=[:id]))
ORDER BY schedule2.Date;
Query 2:
SELECT DISTINCT Count(results.rider_id) AS CountOfrider_id
FROM seasons RIGHT JOIN (results LEFT JOIN schedule2 ON results.schedule_id = schedule2.id) ON seasons.id = schedule2.season_id
GROUP BY schedule2.id, results.class_id
HAVING (((schedule2.id)=[:scheduleId]) AND ((results.class_id)=[:classId]));
The current PHP app produces this result set: (the rider id is picked with a form)
Original PHP (for reference)
$getRiderInfo = $dbh->prepare('
SELECT riders.ridername,
schedule2.date,
schedule2.id AS scheduleId,
results.class_id AS classId,
results.finish,
results.points,
classes.class,
seasons.season,
riders.id
FROM schedule2
RIGHT JOIN (classes
RIGHT JOIN ((riders
RIGHT JOIN results
ON riders.id = results.rider_id)
LEFT JOIN seasons
ON results.season_id = seasons.id)
ON classes.id = results.class_id)
ON schedule2.id = results.schedule_id
GROUP BY riders.ridername,
schedule2.date,
classes.class,
seasons.season
HAVING (((riders.id)=:id))
ORDER BY schedule2.date ASC;
');
$getRiderInfo->bindValue('id', $racer_id);
if ($getRiderInfo->execute()) {
while ($iRows = $getRiderInfo->fetch(PDO::FETCH_ASSOC)) {
$getCounts = $dbh->prepare('
SELECT DISTINCT COUNT(results.rider_id) AS CountOfrider_id,
schedule2.id,
results.class_id
FROM seasons
RIGHT JOIN(results
LEFT JOIN schedule2
ON results.schedule_id = schedule2.id)
ON seasons.id = schedule2.season_id
GROUP BY schedule2.id ,
results.class_id
HAVING (((schedule2.id) = :scheduleId)
AND ((results.class_id) = :classId));
');
$getCounts->bindValue('scheduleId', $iRows['scheduleId']);
$getCounts->bindValue('classId', $iRows['classId']);
if ($getCounts->execute()) {
while ($iRows1 = $getCounts->fetch(PDO::FETCH_ASSOC)) {
$iCount = $iRows1['CountOfrider_id'];
}
}
$iDate = date_create($iRows['date']);
echo '<tr><td>' . $iRows['ridername'] . '</td>';
echo '<td>' . date_format($iDate, 'm/d/Y') . '</td>';
echo '<td>' . $iRows['finish'] . '</td>';
echo '<td>' . $iRows['points'] . '</td>';
echo '<td>' . $iRows['class'] . '</td>';
echo '<td>' . $iRows['season'] . '</td>';
echo '<td>' . $iCount . '</td></tr>';
}
}
I found a better way around this problem. Actually I was kind of over thinking things.
This dataset is never going to change, so, that second query isn't really needed, just ran it once to create a table with the total count I'm looking for for column 6 and added another join to the first query. (too many joins already to keep track of.)
It works for my purpose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.