如何在已选择的字段中选择顶部

时间:2016-01-07 10:29:11

标签: sql sql-server

当我已经从列表中选择了字段时,我想问一下如何添加'select top 1 *'?我在其他代码中看到了一些例子但却没有得到它。如果在我构建的代码中看到它会更容易。

以下是我的查询示例:

select frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId,  per.TitleID,  per.surname,  per.FirstName, per.PersonTypeId, tor.PersonID, tor.Reference
from package pk 
        inner join product p  on p.packageid = pk.packageid
        inner join productperson pp on pp.productid = p.productid
        inner join person per on per.personid = pp.personid 
        left join flightlogicalseat fls on fls.productpersonid = pp.productpersonid
        inner join TourOperatorReference tor  on tor.PersonID = per.PersonId 
        inner join FlightReservationPassenger frp on frp.ProductPersonID = pp.ProductPersonId
where pk.Reference LIKE '%'
and ProductTypeId =1

2 个答案:

答案 0 :(得分:1)

只需尝试使用TOP keyword,就像这样:

select TOP 1 frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId,  per.TitleID,

答案 1 :(得分:1)

您可以将现有查询包装在新查询中:

SELECT TOP 1 * FROM
(select frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId,  per.TitleID,  per.surname,  per.FirstName, per.PersonTypeId, tor.PersonID, tor.Reference
from package pk 
        inner join product p  on p.packageid = pk.packageid
        inner join productperson pp on pp.productid = p.productid
        inner join person per on per.personid = pp.personid 
        left join flightlogicalseat fls on fls.productpersonid = pp.productpersonid
        inner join TourOperatorReference tor  on tor.PersonID = per.PersonId 
        inner join FlightReservationPassenger frp on frp.ProductPersonID = pp.ProductPersonId
where pk.Reference LIKE '%'
and ProductTypeId =1) t