基于多个参数的搜索查询的 SQL 存储过程
好吧,我需要一些关于存储过程的帮助,这有点超出我的能力范围,我不知道如何继续。 SP用于我正在开发的项目中的搜索功能。它是根据最终用户的选择来查找可用座位。用户应该能够根据以下内容进行搜索:
出发机场(或“任何”)
到达机场(或“任何”)
最早出发时间
最晚出发时间
可用的最少座位数
最多返程航班
我最初认为单独的 SELECT 语句就可以了技巧,但正如您可能知道的那样,select 语句被视为单独的查询,并且返回导致 6 个不同的返回。因此,我尝试将它们包含在一个 SELECT 中,但我的逻辑一定很奇怪,因为返回是错误的。如果有人能指出我正确的方向,我将不胜感激。代码如下:
ALTER PROCEDURE [dbo].[usp_FindSeats]
(
@DepartureAirport char(3),
@ArrivalAirport char(3),
@EarliestDepTime datetime,
@LatestDepTime datetime,
@minSeatsAvailable int,
@maxFlightsRequested int
)
AS
BEGIN
SELECT * FROM Flight
WHERE
(@DepartureAirport = UPPER('ANY') OR DepartAirport = @DepartureAirport)
AND
(@ArrivalAirport = UPPER('ANY') OR ArriveAirport = @ArrivalAirport)
AND
(DepartTime >= @EarliestDepTime)
AND
(DepartTime <= @LatestDepTime)
AND
(FlightSeatsAvailiable >= @minSeatsAvailable)
AND
((SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested)
--IF (@DepartureAirport = UPPER('ANY'))
--BEGIN
--(SELECT * FROM Flight WHERE DepartAirport != '')
--END
--ELSE
--SELECT * FROM Flight WHERE DepartAirport = @DepartureAirport
--IF (@ArrivalAirport = UPPER('ANY'))
--Begin
--(SELECT * FROM Flight WHERE ArriveAirport != '')
--END
--ELSE
--SELECT * FROM Flight WHERE ArriveAirport = @ArrivalAirport
--SELECT * FROM Flight WHERE DepartTime >= @EarliestDepTime
--SELECT * FROM Flight WHERE DepartTime <= @LatestDepTime
--SELECT * FROM Flight WHERE FlightSeatsAvailiable >= @minSeatsAvailable
--SELECT * FROM Flight WHERE (SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested
Ok, I need a little help with a stored proc, that is a little bit over my head and I am not sure how to proceed. The SP is for a search function in a project I am working on. It is to find available seats based on selections from the end user. The user should be able to search based on the following:
Departure airport (or "ANY")
Arrival airport (or "ANY")
Earliest Depart time
Latest Depart time
Min Seats Available
Max Flights to be Returned
I initially thought separate SELECT statements would do the trick, but as you probably know, the select statements were treated as separate queries and the return resulted in 6 different returns. So, I attempted to include them in one SELECT, but my logic must be screwy, because the return is faulty. If anyone can point me in the right direction it would be very much appreciated. Code follows:
ALTER PROCEDURE [dbo].[usp_FindSeats]
(
@DepartureAirport char(3),
@ArrivalAirport char(3),
@EarliestDepTime datetime,
@LatestDepTime datetime,
@minSeatsAvailable int,
@maxFlightsRequested int
)
AS
BEGIN
SELECT * FROM Flight
WHERE
(@DepartureAirport = UPPER('ANY') OR DepartAirport = @DepartureAirport)
AND
(@ArrivalAirport = UPPER('ANY') OR ArriveAirport = @ArrivalAirport)
AND
(DepartTime >= @EarliestDepTime)
AND
(DepartTime <= @LatestDepTime)
AND
(FlightSeatsAvailiable >= @minSeatsAvailable)
AND
((SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested)
--IF (@DepartureAirport = UPPER('ANY'))
--BEGIN
--(SELECT * FROM Flight WHERE DepartAirport != '')
--END
--ELSE
--SELECT * FROM Flight WHERE DepartAirport = @DepartureAirport
--IF (@ArrivalAirport = UPPER('ANY'))
--Begin
--(SELECT * FROM Flight WHERE ArriveAirport != '')
--END
--ELSE
--SELECT * FROM Flight WHERE ArriveAirport = @ArrivalAirport
--SELECT * FROM Flight WHERE DepartTime >= @EarliestDepTime
--SELECT * FROM Flight WHERE DepartTime <= @LatestDepTime
--SELECT * FROM Flight WHERE FlightSeatsAvailiable >= @minSeatsAvailable
--SELECT * FROM Flight WHERE (SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解您的需求,最简单的方法是:
SELECT * FROM Flight
在哪里
(@DepartureAirport = UPPER('ANY') 或 DepartureAirport = @DepartureAirport)
联盟
从航班中选择 *
在哪里
(@ArrivalAirport = UPPER('ANY') 或 ArriveAirport = @ArrivalAirport)
友联
从航班中选择 *
在哪里
(出发时间>=@EarliestDepTime)
联盟
从航班中选择 *
在哪里
(出发时间 <= @LatestDepTime)
联盟
从航班中选择 *
在哪里
(FlightSeatsAvailable >= @minSeatsAvailable)
联盟
从航班中选择 *
在哪里
((SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested)
这将根据用户的输入聚合所有结果。
Simplest approach would be, if i understand your need correctly:
SELECT * FROM Flight
WHERE
(@DepartureAirport = UPPER('ANY') OR DepartAirport = @DepartureAirport)
UNION
SELECT * FROM Flight
WHERE
(@ArrivalAirport = UPPER('ANY') OR ArriveAirport = @ArrivalAirport)
AUNION
SELECT * FROM Flight
WHERE
(DepartTime >= @EarliestDepTime)
UNION
SELECT * FROM Flight
WHERE
(DepartTime <= @LatestDepTime)
UNION
SELECT * FROM Flight
WHERE
(FlightSeatsAvailiable >= @minSeatsAvailable)
UNION
SELECT * FROM Flight
WHERE
((SELECT COUNT(FlightID) FROM Flight) <= @maxFlightsRequested)
this would aggregate all the results depending on user's input.
试试这个:
请注意,如果您限制返回的行数,那么以某种方式对结果进行排序(ORDER BY)可能是个好主意。
Try this:
Note that if you limit the number of rows returned, it's probably a good idea to sort the result in some way (ORDER BY).