sql-为什么在使用select语句时会得到重复的数据?
发布时间:2022-06-11 15:52:17 419
相关标签: # 数据库
我在尝试使用 select 语句时得到重复的数据,我有 9 个房间,但在尝试显示它们时我得到了大约 50-70 个房间。请帮忙?
我正在尝试使用 select 语句插入数据并显示它。
create table gym(
GymName VARCHAR(200) primary key,
openTime time not null,
closeTime time not null,
Price decimal not null,
);
create table Spa(
spaName VARCHAR(200) primary key,
openTime time not null,
closeTime time not null,
Price decimal not null,
);
create table customer(
CustomerID int primary key,
Firstname varchar(200) not null,
LastName varchar(200) not null,
DOB date not null check (DATEDIFF(year,DOB,getdate ()) > 18) ,
Gender char(4) not null check(Gender ='M' or Gender = 'F'),
Address varchar(200) not null default 'Jordan',
spaName VARCHAR(200) foreign key references Spa(spaName),
GymName VARCHAR(200) foreign key references gym(GymName),
);
Create table CustomerPhoNo(
CustomerID int foreign key references customer(CustomerID),
PhoneNo bigint not null,
);
create table Room(
roomNo int primary key,
Availability char(4) not null,
NoOfBeds int not null,
Rate int not null,
CheckIn date,
CheckOut date,
Price Decimal not null,
Breakfast char(4),
CustomerID int foreign key references customer(CustomerID),
);
create table LocationOfRoom(
roomNo int foreign key references Room(roomNo),
seaview char(4),
Location varchar(20) not null,
);
create table RoomType(
roomNo int foreign key references Room(roomNo),
familyRoom char(4),
doubleRoom char(4),
singleRoom char(4),
);
create table Gservice(
GymName VARCHAR(200) foreign key references gym(GymName),
Service VARCHAR(500) not null,
MachineType VARCHAR(500) not null,
);
create table PaymentCard(
CardID int primary key,
issueDate date not null,
Expirydate date not null,
CustomerID int foreign key references customer(CustomerID),
);
insert into Spa values ('BeautySpa', '09:00:00', '04:00:00', 60.0)
insert into Spa values ('LushLife', '07:00:00', '01:00:00', 40.0)
insert into Spa values ('RelaxationTherapy', '08:00:00', '03:00:00', 40.0)
--
insert into gym values ('StrongBody', '08:30:00', '03:20:00', 15.0)
insert into gym values ('SilverGym', '05:00:00', '04:30:00', 25.0)
insert into gym values ('FitnessHeroes', '07:00:00', '02:00:00', 20.0)
--
insert into customer values (325,'Mohammad','Alasharan','06-04-1984','M','Amman', 'BeautySpa', 'StrongBody')
insert into customer values (348,'John','Shelby','10-18-1998','M','Birmingham', 'LushLife', 'SilverGym')
insert into customer values (495,'Thomas','Hoffman','04-26-1968','M','Johannesburg', 'RelaxationTherapy', 'SilverGym')
insert into customer values (194,'Anne','Frank','07-22-2001','F','Frankfurt', 'BeautySpa', 'StrongBody')
insert into customer values (628,'Katie','Swan','02-10-1997','F','New South Wales', 'LushLife', 'FitnessHeroes')
insert into customer values (246,'Mahmoud','Alkutaifan','04-21-1994','M','Amman', 'BeautySpa', 'FitnessHeroes')
insert into customer values (864,'Karl-Heinz','Rummenigge','09-25-1955','M','Lippstadt', 'RelaxationTherapy', 'FitnessHeroes')
insert into customer values (824,'Dennis','Law','09-21-1979','M','london', 'RelaxationTherapy', 'FitnessHeroes')
insert into customer values (463,'Carles','Puyol','06-17-1973','M','madrid', 'LushLife', 'FitnessHeroes')
--
insert into CustomerPhoNo values (325,009627863429451)
insert into CustomerPhoNo values (348,009627865489359)
insert into CustomerPhoNo values (495,009627863495472)
insert into CustomerPhoNo values (194,009627863472654)
insert into CustomerPhoNo values (628,009627932648451)
insert into CustomerPhoNo values (246,009627863429728)
insert into CustomerPhoNo values (864,009627824869481)
insert into CustomerPhoNo values (824,009627863428731)
--
insert into Gservice values ('StrongBody','Fitness and cardio','Treadmill and leg press machine and Ergometer')
insert into Gservice values ('FitnessHeroes','Body building and nutrition advice','Chest press machine and Leg Extension Machine and Lat pull down Machine')
insert into Gservice values ('SilverGym','Boot camp and weight loss','Treadmill and stairs simulator machine and Spin Bike')
--
insert into Room values (124,'yes','1','4',null,null,'30','yes',null)
insert into Room values (135,'no','2','5','05-06-2022','05-09-2022','55','yes',495)
insert into Room values (121,'yes','1','3',null,null,'40','yes',null)
insert into Room values (139,'no','3','4','05-10-2022','05-14-2022','110','no',194)
insert into Room values (131,'no','3','3','05-18-2022','05-22-2022','130','yes',348)
insert into Room values (136,'no','4','4','04-14-2022','04-24-2022','120','yes',194)
insert into Room values (179,'yes','4','5',null,null,'95','no',null)
insert into Room values (138,'no','3','3','04-02-2022','04-06-2022','75','no',246)
insert into Room values (146,'no','3','5','05-10-2022','05-14-2022','80','yes',864)
--
insert into LocationOfRoom values (124,'no','south')
insert into LocationOfRoom values (135,'yes','north')
insert into LocationOfRoom values (121,'yes','south')
insert into LocationOfRoom values (139,'no','north')
insert into LocationOfRoom values (131,'no','East')
insert into LocationOfRoom values (136,'yes','west')
insert into LocationOfRoom values (179,'no','south')
insert into LocationOfRoom values (138,'no','west')
insert into LocationOfRoom values (146,'yes','north')
--
insert into PaymentCard values (1,'01-01-2022','12-11-2025', 325)
insert into PaymentCard values (2,'06-01-2018','12-05-2023', 348)
insert into PaymentCard values (3,'07-01-2019','12-17-2023', 495)
insert into PaymentCard values (4,'06-05-2022','08-28-2024', 194)
insert into PaymentCard values (5,'02-01-2020','12-04-2026', 628)
insert into PaymentCard values (6,'06-18-2018','07-11-2022', 246)
insert into PaymentCard values (7,'08-14-2022','03-07-2023', 864 )
--
insert into RoomType values (124,'no','no','yes')
insert into RoomType values (135,'no','yes','no')
insert into RoomType values (121,'yes','no','no')
insert into RoomType values (139,'no','no','yes')
insert into RoomType values (131,'no','no','yes')
insert into RoomType values (136,'no','no','yes')
insert into RoomType values (179,'no','no','yes')
insert into RoomType values (138,'no','yes','no')
insert into RoomType values (146,'no','no','yes')
--
--THE FOLLOWING COMMANDS ARE FOR TASK 1 QUESTION 2
--registration of a new customer account
insert into customer values (726,'Ahmad','Aldosooqi','08-07-1987','M','Aqaba', 'BeautySpa', 'StrongBody')
--deletion of a customer account
DELETE FROM customer WHERE CustomerID=726;
--adding deleting and updating rooms
insert into Room values ('235','yes','1','2','03-16-2022','03-22-2022','55','yes',824)
delete from Room where CustomerID=824;
UPDATE Room
SET Rate = 5
WHERE CustomerID=824;
--updating deleting and adding payment cards and services
insert into PaymentCard values (8,'05-01-2023','04-11-2026', 824)
delete from PaymentCard where CustomerID=824;
UPDATE PaymentCard
SET Expirydate = '05-11-2026'
WHERE CustomerID=824;
-- updating deleting and adding services
insert into Spa values ('NewLook', '07:00:00', '01:00:00',80.0)
delete from Spa where spaName='NewLook';
UPDATE Spa
SET Price = 60.0
WHERE spaName='NewLook';
--Updating the Availability for the rooms for all of them to be available
UPDATE Room
SET Availability = 'yes', CustomerID=null, CheckIn=null, CheckOut=null;
--display all unreserved rooms ordered by number of beds in descending order
select * from Room
where Availability='yes'
order by NoOfBeds desc
--display All double and family rooms with a price below 100$ per night in ascending order of price.
select doubleRoom, familyRoom, Price
from RoomType, Room
where Price<100
order by Price asc
--display The details of all rooms including the name of the customer staying in the room if the room is occupied.
select Firstname,LastName, roomNo, Availability, NoOfBeds, Rate, CheckIn, CheckOut, Price, Breakfast
from Room, customer
where Availability='no'
--display All spa services with name, gender and phone number of registered customers
select spaName, Firstname, LastName, Gender,PhoneNo
from customer, CustomerPhoNo
--display Total revenue per night from all double rooms
SELECT SUM(Price) as TotalRevenue
FROM Room, RoomType
where doubleRoom='yes'
-- display Total number of customers who booked a single room with sea view option
select count(Firstname)
from LocationOfRoom, customer, RoomType,Room
where seaview='yes' and singleRoom='yes'
任何帮助都将不胜感激,提前谢谢!
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报