2015-04-24 60 views
1

我可以查看我的資料有問題,因爲我想在1組合2行,查看和同桌

IDFlight | Dep1 | Des1| Date | IDFlight2 | Dep2 | Des2 | Date | Price 
---------+------+-----+-------+-----------+------+------+--------+-------- 
    2 | AYT | PRN |20.3.15|  3  | PRN | AYT | 27.3.15| 150 
    2 | AYT | PRN |20.3.15|  4  | PRN | AYT | 30.3.15| 150 
    1 | AYT | PRN |23.3.15|  4  | PRN | AYT | 30.3.15| 150 
    1 | AYT | PRN |17.3.15|  3  | PRN | AYT | 27.3.15| 150 

所以搜索查詢與日期+ - 4天都爲2個航班。

在我的情況下,每個航班都單獨登記在航班表中,每個航班都有他的航班號和他的航班號,日期和定價(例如,返程和單程)。 現在這裏的問題是,當用戶選擇返回選項時,將在一行中顯示2個航班(航班1去目的地,航班2從目的地返航),在例子中的日期線之間也有差異圖片。

我想要實現的是顯示上例中的數據,即每個「單向」記錄應該與「返回」記錄匹配。即使第一個數據重複。

我已經做了很多的研究,但沒有結果,也 我試圖用一個看法,但沒有成功

我想聯盟沒有做成功。

@prmDepDay int, @prmDesDay int, @prmDateDep datetime, @prmFrom int, @prmTo int, 
@prmDateRe datetime, @prmFromRe int, @prmToRe int, @prmTotalRe int 
AS 
BEGIN 
DROP TABLE departureflights 

SELECT TOP(100) PERCENT 
      t_flights.idflight, 
      t_flights.flightnumber, 
      t_departureairport.depairportname, 
      t_destinationairport.desairportname, 
      t_flights.startdate, 
      t_flights.totalseats 
    INTO  departureflights 
    FROM  t_flights 
    INNER JOIN t_departureairport 
    ON   t_flights.iddepartureairport = t_departureairport.iddepartureairport 
    INNER JOIN t_destinationairport 
    ON   t_flights.iddestinationairport = t_destinationairport.iddestinationairport 
    INNER JOIN t_flightdirections 
    ON   t_flights.iddirection = t_flightdirections.iddirection 
    WHERE  t_departureairport.iddepartureairport = @prmFrom 
    AND  t_destinationairport.iddestinationairport = @prmTo 
    AND  startdate >= Dateadd(day,[email protected],@prmDateDep) 
    AND  startdate <= Dateadd(day,@prmDepDay,@prmDateDep) 
      --and TotalSeats>= @prmTotal 
    ORDER BY t_flights.startdate 

    DROP TABLE returnflights   

    SELECT  t_flights.idflight, 
      t_flights.flightnumber    AS ReFlightNumber, 
      t_departureairport.depairportname AS ReDepAirportName, 
      t_destinationairport.desairportname AS ReDesAirportName, 
      t_flights.enddate , 
      t_flights.totalseats 
    INTO  returnflights 
    FROM  t_flights 
    INNER JOIN t_departureairport 
    ON   t_flights.iddepartureairport = t_departureairport.iddepartureairport 
    INNER JOIN t_destinationairport 
    ON   t_flights.iddestinationairport = t_destinationairport.iddestinationairport 
    INNER JOIN t_flightdirections 
    ON   t_flights.iddirection = t_flightdirections.iddirection 
    WHERE  t_departureairport.iddepartureairport = @prmFromRe 
    AND  t_destinationairport.iddestinationairport = @prmToRe 
    AND  enddate >= Dateadd(day,[email protected],@prmDateRe) 
    AND  enddate <= Dateadd(day,@prmDesDay,@prmDateRe) 
    AND  totalseats>= @prmTotalRe 
    ORDER BY t_flights.enddate 
+0

也許交叉連接可以幫助你? – Disappointed

+0

您有這篇文章被列爲C#。您可以從C#執行存儲SQL過程,然後返回結果。在C#中,這可以比使用SQL查詢更容易完成。執行連接後,您可以從單個「連接結果」創建多行。如果您可以發佈存儲的SQL過程的結果,我可以提供幫助。 – jdweng

回答

1

如果你自己加入t_flights表,你應該在一行中獲得傳出和返回的航班信息。

SELECT journey_out.idflight IDFlight, 
    journey_out.iddepartureairport Dep1, 
    journey_out.iddestinationairport Des1, 
    journey_out.enddate Date1, 
    journey_return.idflight IDFlight2, 
    journey_return.iddepartureairport Dep2, 
    journey_return.iddestinationairport Dep2, 
    journey_return.enddate Date2 
FROM t_flights journey_out 
INNER JOIN t_flights journey_return 
    ON journey_out.iddestinationairport = journey_return.iddepartureairport 
    AND journey_out.enddate < journey_return.startdate 
ORDER BY journey_out.startdate 

第一個連接條件確保航班從正確的機場回家,第二個條件確保回程在到達之前不會開始。

如果您想要在返回選項中看到相同結果集中的單向選項,可以將其更改爲LEFT JOIN而不是INNER JOIN。

+0

非常感謝。 –