我有一个复杂的SQL查询,它意味着返回所选汽车给出的所有评论,但是,我只会看到如下所示的一个,而不是像屏幕截图二那样显示所有评论。
这是我的DDL
:
CREATE TABLE carsType(
carsTypeID INTEGER NOT NULL AUTO_INCREMENT,
carsTypeName VARCHAR(200) NOT NULL,
PRIMARY KEY(carsTypeID)
);
CREATE TABLE carCategoryType(
carCategoryTypeID INTEGER NOT NULL AUTO_INCREMENT,
carCategoryTypeName VARCHAR(200) NOT NULL,
PRIMARY KEY(carCategoryTypeID)
);
CREATE TABLE cars(
carID INTEGER NOT NULL AUTO_INCREMENT,
carsTypeID INTEGER NOT NULL,
carCategoryTypeID INTEGER NOT NULL,
carName VARCHAR(200),
carArea VARCHAR(200),
FOREIGN KEY(carsTypeID) REFERENCES carsType(carsTypeID),
FOREIGN KEY(carCategoryTypeID) REFERENCES carCategoryType(carCategoryTypeID),
PRIMARY KEY(carID)
);
CREATE TABLE carsReview(
carID INTEGER NOT NULL,
carMileage INTEGER NOT NULL,
reviews VARCHAR(200) NOT NULL,
FOREIGN KEY(carID) REFERENCES cars(carID)
);
INSERT INTO carsType VALUES
(NULL, "Hatchback"),
(NULL, "Saloon"),
(NULL, "Sedan");
INSERT INTO carCategoryType VALUES
(NULL, "A"),
(NULL, "B"),
(NULL, "C");
INSERT INTO cars VALUES
(NULL, 1, 1, "Golf", "Germany"),
(NULL, 2, 2, "Ford", "USA"),
(NULL, 2, 2, "Pontiac", "USA");
INSERT INTO carsReview VALUES
(1, 10000, "Good clean car"),
(2, 3000, "Worth it"),
(3, 1000, "Brand spanking new"),
(1, 45000, "She is good"),
(1, 100000, "Gone son"),
(1, 23000, "Looking fine");
这是我的复杂SQL
查询:
SELECT tbl_cars.carID, tbl_cars.carName, tbl_cars.carArea,
(IFNULL(tbl_carsReview.averageMileage, 0.0)) AS finalaverageMileage , tbl_carsReview.reviews, tbl_carsType.carsTypeName,
tbl_carCategoryType.carCategoryTypeName
FROM cars tbl_cars
INNER JOIN carsType tbl_carsType ON
tbl_cars.carsTypeID = tbl_carsType.carsTypeID
INNER JOIN carCategoryType tbl_carCategoryType ON
tbl_cars.carCategoryTypeID = tbl_carCategoryType.carCategoryTypeID
LEFT JOIN (SELECT carsReview.carID, ROUND(AVG(carsReview.reviews),1) AS averageMileage, carsReview.reviews
FROM carsReview) tbl_carsReview
ON tbl_cars.carID = tbl_carsReview.carID
WHERE tbl_cars.carID = 1
这是我的sqlfiddle
:http://sqlfiddle.com/#!9/c9a92c/8
答案 0 :(得分:2)
您在子查询中缺少group by
:
SELECT tbl_cars.carID, tbl_cars.carName, tbl_cars.carArea,
(IFNULL(tbl_carsReview.averageMileage, 0.0)) AS finalaverageMileage ,
tbl_carsReview.reviews, tbl_carsType.carsTypeName,
tbl_carCategoryType.carCategoryTypeName
FROM cars tbl_cars INNER JOIN
carsType tbl_carsType
ON tbl_cars.carsTypeID = tbl_carsType.carsTypeID INNER JOIN
carCategoryType tbl_carCategoryType
ON tbl_cars.carCategoryTypeID = tbl_carCategoryType.carCategoryTypeID LEFT JOIN
(SELECT carsReview.carID, ROUND(AVG(carsReview.reviews),1) AS averageMileage, carsReview.reviews
FROM carsReview
GROUP BY carsReview.carID
------^
) tbl_carsReview
ON tbl_cars.carID = tbl_carsReview.carID
WHERE tbl_cars.carID = 1;
编辑:
如果您希望评论和的平均值,则需要额外的join
:
SELECT tbl_cars.carID, tbl_cars.carName, tbl_cars.carArea,
(IFNULL(tbl_carsReviewAvg.averageMileage, 0.0)) AS finalaverageMileage ,
tbl_carsReview.reviews, tbl_carsType.carsTypeName,
tbl_carCategoryType.carCategoryTypeName
FROM cars tbl_cars INNER JOIN
carsType tbl_carsType
ON tbl_cars.carsTypeID = tbl_carsType.carsTypeID INNER JOIN
carCategoryType tbl_carCategoryType
ON tbl_cars.carCategoryTypeID = tbl_carCategoryType.carCategoryTypeID LEFT JOIN
(SELECT carsReview.carID, ROUND(AVG(carsReview.reviews),1) AS averageMileage, carsReview.reviews
FROM carsReview
GROUP BY carsReview.carID
) tbl_carsReviewAvg
ON tbl_cars.carID = tbl_carsReviewAvg.carID LEFT JOIN
carsReview tbl_carsReview
ON tbl_cars.carID = tbl_carsReview.carID
WHERE tbl_cars.carID = 1;