2011-01-12 23 views
0

我有兩個表我怎麼可以做以下查詢來獲取所需的信息

  • CompList表下面的列:CompIdMcIDstationslotsubslot,和其他幾個人

  • BookingTable列:CompId,LineID,McID,station,slot,subslot

我希望得到以下結果:只有

行時,CompList.CompId == BookingTable.CompId(僅CompId是兩個表中)

我在結果列需要從CompListCompIdMcIDstationslotsubslot和從BookingTableLineIDMcIDstationslotsubslot

我將如何區分結果表中的結果表中的同一列表中的同一列?

感謝您的幫助。

+0

你可以在http://dba.stackexchange.com/上詢問 – 2011-01-12 07:50:03

回答

2

使用別名:

SELECT 
    CL.CompId, 
    CL.McID, 
    CL.station, 
    CL.slot, 
    CL.subslot, 
    BT.LineID, 
    BT.McID  as BookingMcId, 
    BT.station as BookingStation, 
    BT.slot  as BookingSlot, 
    BT.subslot as BookingSubslot 
FROM CompList as CL 
JOIN BookingTable as BT ON BT.CompId = CL.CompId 
0
select c.CompId,c.D,c.station,c.slot,c.subslot,b.neID,b.McID,b.station,b.slot,b.subslot from CompList c join BookingTable b on c.ComId=b.CompId 
0
SELECT 
    cl.CompId , cl.McID , cl.station, cl.slot, cl.subslot, 
    bt.LineID, bt.McID, bt.station, bt.slot, bt.subslot 
FROM 
    CompList cl 
INNER JOIN 
    BookingTable bt ON cl.McID=bt.McID 
0

根據你的問題,區分CompList和BookingTable使用別名的列要在結果得到的列。例如:假設有兩個表t1 & t2,它們在cols中都有相同的名稱,如cid,name,roll和address。

讓我們寫t1.cid == t2.cid(即兩個表中唯一的CID)

:選擇t1.cid, t1.name爲NAME1, t1.roll爲ROLL1, t1.address as address1, t2.name as name2, t2.roll as roll2, t2。從t1開始的地址爲 ,t2 其中t1.cid = t2.cid;

相關問題