我在这里遇到一个问题:“显示每天由巴黎汽车驾驶的平均距离”
我还有2个表引用了这个问题
table_cars
:id
,brand
,type
,license
table_distances
:id_car
,date
,distance
我设法选择“巴黎汽车的平均距离”
select avg(table_distances.distance)
from table_distances
INNER JOIN table_cars ON table_distances.id_car = table_cars.id
where table_cars.license = 'Paris';'
尽管如此,我每天的平均距离仍有问题。我查看了stackoverflow / google上的相关问题,但我更加困惑。
有人可以解释我如何改进查询以显示每天的平均距离吗?
答案 0 :(得分:0)
只需将日期添加到您选择的内容并按日期分组,以便按行平均:
SELECT table_distances.date, avg(table_distances.distance)
FROM table_distances
INNER JOIN table_cars ON table_distances.id_car = table_cars.id
WHERE table_cars.license = 'Paris'
GROUP BY table_distances.date
答案 1 :(得分:0)
这可以让你了解每个车每个日期的距离。
SELECT id_car, date, AVG(table_distances.distance)
FROM table_distances
INNER JOIN table_cars
ON table_distances.id_car = table_cars.id
WHERE table_cars.license = 'Paris'
GROUP BY id_car, date
ORDER BY id_car, date
答案 2 :(得分:0)
只需将GROUP BY
子句添加到DATE列
SELECT AVG(td.distance)
FROM table_distances td
INNER JOIN table_cars tc ON td.id_car = tc.id
WHERE tc.license = 'Paris'
GROUP BY td.date;
然后你必须得到每天一辆车的平均距离