我一直在努力,非常感谢一些帮助:
我有两张桌子车和游乐设施
ruby http://www.example.com/script.rb
汽车输出:create table cars
(cars_ID Int (10),
manuf varchar (30),
model varchar (30));
create table rides
(ride_ID Int (10),
cars_ID varchar (30),
ride_dest varchar (30));
insert into cars (cars_ID, manuf, model) values (1, 'Honda', 'CRV');
insert into cars (cars_ID, manuf, model) values (2, 'Ford', 'Fusion');
insert into cars (cars_ID, manuf, model) values (3, 'Toyota', 'Corolla');
insert into cars (cars_ID, manuf, model) values (4, 'Toyota', 'Camry');
insert into cars (cars_ID, manuf, model) values (5, 'Ford', 'Fusion');
insert into cars (cars_ID, manuf, model) values (6, 'Ford', 'Fusion');
insert into cars (cars_ID, manuf, model) values (7, 'Ford', 'Fusion');
insert into cars (cars_ID, manuf, model) values (8, 'Ford', 'Fusion');
insert into cars (cars_ID, manuf, model) values (9, 'Ford', 'Fusion');
insert into rides (ride_ID, cars_ID, ride_dest) values (1, 3, 'Boston');
insert into rides (ride_ID, cars_ID, ride_dest) values (2, 5, 'New York');
insert into rides (ride_ID, cars_ID, ride_dest) values (3, 5, 'Washington DC');
insert into rides (ride_ID, cars_ID, ride_dest) values (4, 1, 'California');
insert into rides (ride_ID, cars_ID, ride_dest) values (5, 2, 'Dallas');
insert into rides (ride_ID, cars_ID, ride_dest) values (6, 5, 'Canada');
insert into rides (ride_ID, cars_ID, ride_dest) values (7, 5, 'Canada');
insert into rides (ride_ID, cars_ID, ride_dest) values (8, 1, 'Canada');
insert into rides (ride_ID, cars_ID, ride_dest) values (9, 5, 'Canada');
insert into rides (ride_ID, cars_ID, ride_dest) values (10, 3, 'Canada');
select * from cars;
游乐设施的输出:cars_ID-manuf-model
1-Honda-CRV
2-Ford-Fusion
3-Toyota-Corolla
4-Toyota-Camry
5-Ford-Fusion
6-Ford-Fusion
7-Ford-Fusion
8-Ford-Fusion
9-Ford-Fusion
select * from rides;
我想计算每种车型的乘坐次数(car_manuf和car_model的组合)输出应该显示每种车型的乘坐总和,并且应该从最多到最少的乘坐次数进行排序。
输出应为:
ride_ID-cars_ID-ride_dest
1-3-Boston
2-5-New York
3-5-Washington
4-1-California
5-2-Dallas
6-5-Canada
7-6-Canada
8-7-Canada
9-8-Canada
10-9-Canada
mycode:没有GROUP BY
CarType-NumberofRides
Ford-Fusion - 8
Toyota-Corolla - 1
Honda-CRV - 1
Toyota-Camry - 0
myOutput:没有GROUP BY
SELECT CONCAT(c.manuf, '-', c.model) AS 'CarType',
(select count(*) from rides r where r.cars_ID = c.cars_ID) AS 'NumberOfRides'
from cars c
order by NumberofRides ASC;
mycode:with GROUP BY
CarType-NumberOfRides
Toyota-Camry - 0
Toyota-Corolla - 1
Ford-Fusion - 1
Honda-CRV - 1
myOutput:没有GROUP BY
SELECT CONCAT(c.manuf, '-', c.model) AS 'CarType',
(select count(*) from rides r where r.cars_ID = c.cars_ID) AS 'NumberOfRides'
from cars c
GROUP BY CarType
order by NumberofRides ASC;
这不是我要找的输出。相反,所有CarType都应显示游乐设施的总数。
正确的输出是:
CarType-NumberOfRides
Toyota-Camry - 0
Toyota-Corolla - 1
Ford-Fusion - 1
Ford-Fusion - 1
Ford-Fusion - 1
Honda-CRV - 1
Ford-Fusion - 1
Ford-Fusion - 1
Ford-Fusion - 3
如何获得正确的输出?我是否必须使用SUM,如果是这样,它将如何放置?
答案 0 :(得分:1)
您不需要子查询,您应该加入这两个表。
SELECT CONCAT(c.manuf, '-', c.model) AS CarType, COUNT(r.ride_ID) AS NumberofRides
FROM cars AS c
LEFT JOIN rides AS r ON r.cars_ID = c.cars_ID
GROUP BY CarType
ORDER BY NumberofRides DESC
LEFT JOIN
确保没有游乐设施的汽车将包含在联接中。这些行中的相应ride_ID
将为NULL
,而COUNT(r.ride_ID)
不会计算空值。