SQL 查询 returns 只有 1 行,连接三个以上的表的正确方法是什么?
SQL query returns only 1 row and what is the correct way to join more than three tables?
我正在开发这个酒店预订系统。
我有多个 table 客户、预订、账单、房间、房型、代理。
我正在收据。
所有这些 table 都已连接,但我只收到一行信息,而不是三行收据信息。
我只是一个初学者,如果我的查询不够复杂或经验不足,请多多包涵。
顺便说一句,我正在使用 sqlfiddle。
这是我的查询:
SELECT r.bill_ID
, c.name
, b.payment_type
, b.card_number
, a.agency_name
, mt.description
, SUM(x.fee) Amenities
, mt.room_rate Room
FROM reservation r
JOIN bill b
ON b.bill_ID = r.bill_ID
JOIN agency a
ON a.agency_ID = r.agency_ID
JOIN room m
ON m.room_ID = r.room_ID
JOIN amenities x
ON b.amenity_ID = x.amenity_ID
JOIN customer c
ON c.cust_ID = r.cust_ID
JOIN roomType mt
ON mt.type_ID = m.type_ID;
这是我的 table:
CREATE TABLE roomType(
type_ID varchar(15) PRIMARY KEY,
description varchar(45),
room_rate int);
CREATE TABLE amenities(
amenity_ID varchar(15) PRIMARY KEY,
amenity_name varchar(45),
fee decimal(4,1));
CREATE TABLE agency(
agency_ID varchar(15) PRIMARY KEY,
agency_name varchar(45));
CREATE TABLE customer(
cust_ID varchar(15) PRIMARY KEY,
name varchar(45),
address varchar(45),
contact_no varchar(45),
email varchar(45),
agency_ID varchar(15),
FOREIGN KEY (agency_ID) REFERENCES agency(agency_ID));
CREATE TABLE bill(
bill_ID varchar(15) PRIMARY KEY,
cust_ID varchar(15),
amenity_ID varchar(15),
agency_ID varchar(15),
payment_type varchar(5),
card_number int,
amount int,
FOREIGN KEY (agency_ID) REFERENCES agency(agency_ID),
FOREIGN KEY (amenity_ID) REFERENCES amenities(amenity_ID),
FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID));
CREATE TABLE room(
room_ID varchar(15) PRIMARY KEY,
type_ID varchar(15),
room_num int,
max_guest int,
availability varchar(15),
FOREIGN KEY (type_ID) REFERENCES roomType(type_ID));
CREATE TABLE reservation(
rsrvtn_ID varchar(15) PRIMARY KEY,
bill_ID varchar(15),
cust_ID varchar(15),
room_ID varchar(15),
agency_ID varchar(15),
check_in date,
check_out date,
num_guest int,
book_status varchar(10),
FOREIGN KEY (agency_ID) REFERENCES agency(agency_ID),
FOREIGN KEY (bill_ID) REFERENCES bill(bill_ID),
FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID),
FOREIGN KEY (room_ID) REFERENCES room(room_ID));
CREATE TABLE administrator(
admin_ID varchar(15) PRIMARY KEY,
admin_lvl varchar(15),
rsrvtn_ID varchar(15),
bill_ID varchar(15),
room_ID varchar(15),
cust_ID varchar(15),
FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID),
FOREIGN KEY (rsrvtn_ID) REFERENCES reservation(rsrvtn_ID),
FOREIGN KEY (bill_ID) REFERENCES bill(bill_ID),
FOREIGN KEY (room_ID) REFERENCES room(room_ID));
您的查询有一个 SUM()
使其成为聚合查询。没有 GROUP BY
returns 正好一行的聚合查询。或者错误。
您的查询应该返回一个错误。为什么? SELECT
列与 GROUP BY
列不一致。所有未聚合的列都应该在 GROUP BY
:
GROUP BY r.bill_ID, c.name, b.payment_type, b.card_number,
a.agency_name, mt.description, mt.room_rate
这是常识。大多数数据库都需要它。它甚至是 MySQL 支持的当前版本。
不幸的是,曾几何时,MySQL 支持您问题中的语法。从 MySQL 8.0 开始,这不再是默认行为。它由系统设置 only_full_group_by
控制,在 documentation.
中进行了解释
我正在开发这个酒店预订系统。
我有多个 table 客户、预订、账单、房间、房型、代理。
我正在收据。
所有这些 table 都已连接,但我只收到一行信息,而不是三行收据信息。
我只是一个初学者,如果我的查询不够复杂或经验不足,请多多包涵。
顺便说一句,我正在使用 sqlfiddle。
这是我的查询:
SELECT r.bill_ID
, c.name
, b.payment_type
, b.card_number
, a.agency_name
, mt.description
, SUM(x.fee) Amenities
, mt.room_rate Room
FROM reservation r
JOIN bill b
ON b.bill_ID = r.bill_ID
JOIN agency a
ON a.agency_ID = r.agency_ID
JOIN room m
ON m.room_ID = r.room_ID
JOIN amenities x
ON b.amenity_ID = x.amenity_ID
JOIN customer c
ON c.cust_ID = r.cust_ID
JOIN roomType mt
ON mt.type_ID = m.type_ID;
这是我的 table:
CREATE TABLE roomType(
type_ID varchar(15) PRIMARY KEY,
description varchar(45),
room_rate int);
CREATE TABLE amenities(
amenity_ID varchar(15) PRIMARY KEY,
amenity_name varchar(45),
fee decimal(4,1));
CREATE TABLE agency(
agency_ID varchar(15) PRIMARY KEY,
agency_name varchar(45));
CREATE TABLE customer(
cust_ID varchar(15) PRIMARY KEY,
name varchar(45),
address varchar(45),
contact_no varchar(45),
email varchar(45),
agency_ID varchar(15),
FOREIGN KEY (agency_ID) REFERENCES agency(agency_ID));
CREATE TABLE bill(
bill_ID varchar(15) PRIMARY KEY,
cust_ID varchar(15),
amenity_ID varchar(15),
agency_ID varchar(15),
payment_type varchar(5),
card_number int,
amount int,
FOREIGN KEY (agency_ID) REFERENCES agency(agency_ID),
FOREIGN KEY (amenity_ID) REFERENCES amenities(amenity_ID),
FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID));
CREATE TABLE room(
room_ID varchar(15) PRIMARY KEY,
type_ID varchar(15),
room_num int,
max_guest int,
availability varchar(15),
FOREIGN KEY (type_ID) REFERENCES roomType(type_ID));
CREATE TABLE reservation(
rsrvtn_ID varchar(15) PRIMARY KEY,
bill_ID varchar(15),
cust_ID varchar(15),
room_ID varchar(15),
agency_ID varchar(15),
check_in date,
check_out date,
num_guest int,
book_status varchar(10),
FOREIGN KEY (agency_ID) REFERENCES agency(agency_ID),
FOREIGN KEY (bill_ID) REFERENCES bill(bill_ID),
FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID),
FOREIGN KEY (room_ID) REFERENCES room(room_ID));
CREATE TABLE administrator(
admin_ID varchar(15) PRIMARY KEY,
admin_lvl varchar(15),
rsrvtn_ID varchar(15),
bill_ID varchar(15),
room_ID varchar(15),
cust_ID varchar(15),
FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID),
FOREIGN KEY (rsrvtn_ID) REFERENCES reservation(rsrvtn_ID),
FOREIGN KEY (bill_ID) REFERENCES bill(bill_ID),
FOREIGN KEY (room_ID) REFERENCES room(room_ID));
您的查询有一个 SUM()
使其成为聚合查询。没有 GROUP BY
returns 正好一行的聚合查询。或者错误。
您的查询应该返回一个错误。为什么? SELECT
列与 GROUP BY
列不一致。所有未聚合的列都应该在 GROUP BY
:
GROUP BY r.bill_ID, c.name, b.payment_type, b.card_number,
a.agency_name, mt.description, mt.room_rate
这是常识。大多数数据库都需要它。它甚至是 MySQL 支持的当前版本。
不幸的是,曾几何时,MySQL 支持您问题中的语法。从 MySQL 8.0 开始,这不再是默认行为。它由系统设置 only_full_group_by
控制,在 documentation.