MYSQL 在执行 GROUP BY 和从 4 个数据库表中获取数据时遇到问题
MYSQL Having trouble doing a GROUP BY and getting data from 4 database tables
我有 4 个数据库表 Rental Car Company Database、Rental Car Details Database、Rental Car Rates Database 和 Booked Car Database。
有多家租车公司和地点有多辆汽车,每辆汽车的费率范围不同,
我想做的是查询所有这些数据库,找出哪些位置只有 03/04/22 - 03/10/22 可用的卡车。
我希望每个位置都显示每个位置有多少卡车可用,以及最低每日费率和最高每日费率。
问题是每辆卡车都有不同的价格,所以当我按位置进行 GROUP BY 时,当有两辆卡车可用时,它只给出一辆卡车的高价和低价。我的低点应该是我搜索的日期中两者中最低的日率,高点应该是两者中最高的日率。
我用下面的数据和代码得到的结果是:
Location
CarCount
Type
RentalCarCompanyName
lowrate
Highrate
LosAngeles
1
Truck
Car Lux
281
281
NewYork
2
Truck
Discount Car Rental
125
125
我想得到的是:
Location
CarCount
Type
RentalCarCompanyName
lowrate
Highrate
LosAngeles
1
Truck
Car Lux
281
281
NewYork
2
Truck
Discount Car Rental
125
169
我有四个数据库表如下
租车公司数据库
Table 姓名:(地理位置)
Location
Address
CompanyName
NewYork
Queens, NY 11430
Discount Car Rental
LosAngeles
1 World Way, Los Angeles, CA 90045
Car Lux
租车详情数据库
Table 姓名:(汽车)
Location
Car
Type
NewYork
NYCar1
Truck
NewYork
NYCar2
Truck
NewYork
NYCar3
Car
NewYork
NYCar4
Truck
LosAngeles
LACar1
Truck
LosAngeles
LACar2
Truck
LosAngeles
LACar3
Truck
LosAngeles
LACar4
Van
租车费率数据库
Table 姓名:(价格)
Car
fromdate
todate
dayrate
NYCar1
2022-01-01
2022-04-01
199
NYCar1
2022-04-01
2022-07-01
499
NYCar1
2022-07-01
2022-12-31
300
NYCar2
2022-01-01
2022-04-01
125
NYCar2
2022-04-01
2022-07-01
399
NYCar2
2022-07-01
2022-12-31
200
NYCar3
2022-01-01
2022-04-01
169
NYCar3
2022-04-01
2022-07-01
267
NYCar3
2022-07-01
2022-12-31
250
NYCar4
2022-01-01
2022-04-01
169
NYCar4
2022-04-01
2022-07-01
267
NYCar4
2022-07-01
2022-12-31
250
LACar1
2022-01-01
2022-04-01
281
LACar1
2022-04-01
2022-07-01
267
LACar1
2022-07-01
2022-12-31
267
LACar2
2022-01-01
2022-04-01
300
LACar2
2022-04-01
2022-07-01
250
LACar2
2022-07-01
2022-12-31
267
LACar3
2022-01-01
2022-04-01
500
LACar3
2022-04-01
2022-07-01
700
LACar3
2022-07-01
2022-12-31
400
LACar4
2022-01-01
2022-04-01
500
LACar4
2022-04-01
2022-07-01
700
LACar4
2022-07-01
2022-12-31
400
订车数据库
Table 姓名:(预订)
Car
ArrivalDate
DepartureDate
Name
NYCar1
2022-03-01
2022-03-19
Mike Smith
LACar2
2022-03-08
2022-03-16
Joe Luck
LACar3
2022-03-05
2022-03-25
Kelly Johnson
下面是我试过的代码
SELECT Cars.Location, COUNT(Cars.Location) AS CarCount, Cars.Type,
(SELECT CompanyName
FROM geolocations
WHERE geolocations.Location = Cars.Location) AS RentalCarCompanyName,
(SELECT MIN(dayrate)
FROM prices
WHERE Car = Cars.Car AND fromdate <= '2022-03-14' AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' LIMIT 1) AS lowrate,
(SELECT MAX(dayrate)
FROM prices
WHERE Car = Cars.Car AND fromdate <= '2022-03-14' AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' LIMIT 1) AS highrate
FROM Cars
WHERE Car NOT IN (
SELECT Car
FROM bookings
WHERE (
ArrivalDate <= '2022-03-14' AND
DepartureDate > '2022-03-10'
)
) AND Cars.Type = 'TRUCK'
GROUP BY Location
ORDER BY Location ASC
我有一个 fiddle 已加载数据
这是您更新后的查询
SELECT
`cars`.`Location`,
COUNT(`cars`.`Location`) AS CarCount,
`cars`.`Type`,
`geolocations`.`CompanyName`,
MIN(`prices`.`dayrate`) AS lowrate,
MAX(`prices`.`dayrate`) AS highrate
FROM
`cars`
JOIN `geolocations` ON `cars`.`Location` = `geolocations`.`Location`
JOIN `prices` ON `cars`.`Car` = `prices`.`Car`
WHERE
`cars`.Car NOT IN (
SELECT
Car
FROM
bookings
WHERE
(
ArrivalDate <= '2022-03-14'
AND DepartureDate > '2022-03-10'
)
)
AND Cars.Type = 'TRUCK'
AND `prices`.`fromdate` <= '2022-03-14'
AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10'
GROUP BY
Location
ORDER BY
Location ASC
我有 4 个数据库表 Rental Car Company Database、Rental Car Details Database、Rental Car Rates Database 和 Booked Car Database。
有多家租车公司和地点有多辆汽车,每辆汽车的费率范围不同,
我想做的是查询所有这些数据库,找出哪些位置只有 03/04/22 - 03/10/22 可用的卡车。
我希望每个位置都显示每个位置有多少卡车可用,以及最低每日费率和最高每日费率。
问题是每辆卡车都有不同的价格,所以当我按位置进行 GROUP BY 时,当有两辆卡车可用时,它只给出一辆卡车的高价和低价。我的低点应该是我搜索的日期中两者中最低的日率,高点应该是两者中最高的日率。
我用下面的数据和代码得到的结果是:
Location | CarCount | Type | RentalCarCompanyName | lowrate | Highrate |
---|---|---|---|---|---|
LosAngeles | 1 | Truck | Car Lux | 281 | 281 |
NewYork | 2 | Truck | Discount Car Rental | 125 | 125 |
我想得到的是:
Location | CarCount | Type | RentalCarCompanyName | lowrate | Highrate |
---|---|---|---|---|---|
LosAngeles | 1 | Truck | Car Lux | 281 | 281 |
NewYork | 2 | Truck | Discount Car Rental | 125 | 169 |
我有四个数据库表如下
租车公司数据库 Table 姓名:(地理位置)
Location | Address | CompanyName |
---|---|---|
NewYork | Queens, NY 11430 | Discount Car Rental |
LosAngeles | 1 World Way, Los Angeles, CA 90045 | Car Lux |
租车详情数据库 Table 姓名:(汽车)
Location | Car | Type |
---|---|---|
NewYork | NYCar1 | Truck |
NewYork | NYCar2 | Truck |
NewYork | NYCar3 | Car |
NewYork | NYCar4 | Truck |
LosAngeles | LACar1 | Truck |
LosAngeles | LACar2 | Truck |
LosAngeles | LACar3 | Truck |
LosAngeles | LACar4 | Van |
租车费率数据库 Table 姓名:(价格)
Car | fromdate | todate | dayrate |
---|---|---|---|
NYCar1 | 2022-01-01 | 2022-04-01 | 199 |
NYCar1 | 2022-04-01 | 2022-07-01 | 499 |
NYCar1 | 2022-07-01 | 2022-12-31 | 300 |
NYCar2 | 2022-01-01 | 2022-04-01 | 125 |
NYCar2 | 2022-04-01 | 2022-07-01 | 399 |
NYCar2 | 2022-07-01 | 2022-12-31 | 200 |
NYCar3 | 2022-01-01 | 2022-04-01 | 169 |
NYCar3 | 2022-04-01 | 2022-07-01 | 267 |
NYCar3 | 2022-07-01 | 2022-12-31 | 250 |
NYCar4 | 2022-01-01 | 2022-04-01 | 169 |
NYCar4 | 2022-04-01 | 2022-07-01 | 267 |
NYCar4 | 2022-07-01 | 2022-12-31 | 250 |
LACar1 | 2022-01-01 | 2022-04-01 | 281 |
LACar1 | 2022-04-01 | 2022-07-01 | 267 |
LACar1 | 2022-07-01 | 2022-12-31 | 267 |
LACar2 | 2022-01-01 | 2022-04-01 | 300 |
LACar2 | 2022-04-01 | 2022-07-01 | 250 |
LACar2 | 2022-07-01 | 2022-12-31 | 267 |
LACar3 | 2022-01-01 | 2022-04-01 | 500 |
LACar3 | 2022-04-01 | 2022-07-01 | 700 |
LACar3 | 2022-07-01 | 2022-12-31 | 400 |
LACar4 | 2022-01-01 | 2022-04-01 | 500 |
LACar4 | 2022-04-01 | 2022-07-01 | 700 |
LACar4 | 2022-07-01 | 2022-12-31 | 400 |
订车数据库 Table 姓名:(预订)
Car | ArrivalDate | DepartureDate | Name |
---|---|---|---|
NYCar1 | 2022-03-01 | 2022-03-19 | Mike Smith |
LACar2 | 2022-03-08 | 2022-03-16 | Joe Luck |
LACar3 | 2022-03-05 | 2022-03-25 | Kelly Johnson |
下面是我试过的代码
SELECT Cars.Location, COUNT(Cars.Location) AS CarCount, Cars.Type,
(SELECT CompanyName
FROM geolocations
WHERE geolocations.Location = Cars.Location) AS RentalCarCompanyName,
(SELECT MIN(dayrate)
FROM prices
WHERE Car = Cars.Car AND fromdate <= '2022-03-14' AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' LIMIT 1) AS lowrate,
(SELECT MAX(dayrate)
FROM prices
WHERE Car = Cars.Car AND fromdate <= '2022-03-14' AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10' LIMIT 1) AS highrate
FROM Cars
WHERE Car NOT IN (
SELECT Car
FROM bookings
WHERE (
ArrivalDate <= '2022-03-14' AND
DepartureDate > '2022-03-10'
)
) AND Cars.Type = 'TRUCK'
GROUP BY Location
ORDER BY Location ASC
我有一个 fiddle 已加载数据
这是您更新后的查询
SELECT
`cars`.`Location`,
COUNT(`cars`.`Location`) AS CarCount,
`cars`.`Type`,
`geolocations`.`CompanyName`,
MIN(`prices`.`dayrate`) AS lowrate,
MAX(`prices`.`dayrate`) AS highrate
FROM
`cars`
JOIN `geolocations` ON `cars`.`Location` = `geolocations`.`Location`
JOIN `prices` ON `cars`.`Car` = `prices`.`Car`
WHERE
`cars`.Car NOT IN (
SELECT
Car
FROM
bookings
WHERE
(
ArrivalDate <= '2022-03-14'
AND DepartureDate > '2022-03-10'
)
)
AND Cars.Type = 'TRUCK'
AND `prices`.`fromdate` <= '2022-03-14'
AND DATE_ADD(todate, INTERVAL 1 DAY) > '2022-03-10'
GROUP BY
Location
ORDER BY
Location ASC