C# 避免SQL查询、保留应用程序上的重复值
我第一次在这里提问时,我正在使用Visual Studio 2015在C上编写预订应用程序,但我在数据网格视图上显示免费房间时遇到问题,我使用的查询如下:C# 避免SQL查询、保留应用程序上的重复值,c#,mysql,sql,C#,Mysql,Sql,我第一次在这里提问时,我正在使用Visual Studio 2015在C上编写预订应用程序,但我在数据网格视图上显示免费房间时遇到问题,我使用的查询如下: SELECT clientID, cost, arrival, roomNumber, resvNum, departure, size FROM roomswithresvView WHERE (roomNumber NOT IN (SELECT roomNumber FROM roomswithr
SELECT clientID, cost, arrival, roomNumber, resvNum, departure, size
FROM roomswithresvView
WHERE (roomNumber NOT IN
(SELECT roomNumber
FROM roomswithresvView AS roomswithresvView_1
WHERE (arrival BETWEEN @date1 AND @date2)
OR (departure BETWEEN @date1 AND @date2)))
问题是,如果一个房间有多个预订,查询将多次显示它,我尝试使用DISTINCT,但我只能使用一列进行工作,而无法按工作分组
谢谢你的关注
例如,如果我用2016-07-06作为日期1,2016-07-07作为日期2测试查询,它将重复1005房间,因为它在数据库上有两个预订。您将不同的房间放在哪里 你需要一张放房间的桌子和一张预定的桌子。然后需要一个子查询来查找与请求日期冲突的预订。这就是使用DISTINCT的地方。然后需要一个外部查询来查找子查询中未返回的所有房间。别忘了,你有一个在你要求的日期之前开始和之后结束的现有预订!把这些放在一起,你会得到这个
insert into room(costPerNight, roomNumber, size)
values
(55, 1, 13),
(65, 2, 15),
(85, 3, 20)
;
create table reservation(
id int identity (1,1) not null,
roomId int not null,
dateIn date not null,
dateOut date not null
)
insert into reservation (roomId, dateIn, dateOut)
values
(1,'2016-07-01','2016-07-03'),
(1,'2016-07-05','2016-07-08'),
(2,'2016-07-01','2016-07-08')
*/
declare @requestedDateIn date = '2016-07-03'
declare @requestedDateOut date = '2016-07-05';
select * from room where id not in(
--find clashing reservations
select distinct roomId from reservation where
(dateOut > @requestedDateIn and dateOut < @requestedDateOut)
or (dateIn > @requestedDateIn and dateIn < @requestedDateOut)
or (dateIn < @requestedDateIn and dateOut > @requestedDateOut)
)
你把电话放在哪里了 你需要一张放房间的桌子和一张预定的桌子。然后需要一个子查询来查找与请求日期冲突的预订。这就是使用DISTINCT的地方。然后需要一个外部查询来查找子查询中未返回的所有房间。别忘了,你有一个在你要求的日期之前开始和之后结束的现有预订!把这些放在一起,你会得到这个
insert into room(costPerNight, roomNumber, size)
values
(55, 1, 13),
(65, 2, 15),
(85, 3, 20)
;
create table reservation(
id int identity (1,1) not null,
roomId int not null,
dateIn date not null,
dateOut date not null
)
insert into reservation (roomId, dateIn, dateOut)
values
(1,'2016-07-01','2016-07-03'),
(1,'2016-07-05','2016-07-08'),
(2,'2016-07-01','2016-07-08')
*/
declare @requestedDateIn date = '2016-07-03'
declare @requestedDateOut date = '2016-07-05';
select * from room where id not in(
--find clashing reservations
select distinct roomId from reservation where
(dateOut > @requestedDateIn and dateOut < @requestedDateOut)
or (dateIn > @requestedDateIn and dateIn < @requestedDateOut)
or (dateIn < @requestedDateIn and dateOut > @requestedDateOut)
)
但你是在要求预订。你应该删除这个问题,然后用样本数据和期望的结果问另一个问题。你能提到哪一列是不同的吗?谢谢,我一直在roomNumber上测试它,这就是我可以看到的,当一个房间在你的桌子上重复时,房间号已经不是唯一的了…对不起,我认为你需要发布一个示例输出,以便人们能够理解你到底需要什么,但你是在要求预订。你应该删除这个问题,然后用样本数据和期望的结果问另一个问题。你能提到哪一列是不同的吗?谢谢,我一直在roomNumber上测试它,这就是我可以看到的,当一个房间在你的桌子上重复时,房间号已经不是唯一的了…对不起,我认为你需要发布一个示例输出,以便人们能够理解你到底需要什么