SQL在sql子查询中的顺序依次为

时间:2017-03-28 17:46:06

标签: sql sql-server

这是我的查询

SELECT * FROM Place WHERE Place.Id IN (
    SELECT TOP 10 PlaceId from #ResultPlaceList order by CPlaceId desc) 

我的结果是按Place.ID排序的,但我希望得到一个订购CPlaceId的结果。

4 个答案:

答案 0 :(得分:2)

您可以使用联接来使用cPlaceId列进行排序:

select p.*
from Place p
join (
    select top 10 PlaceId,
        CPlaceId
    from #ResultPlaceList
    order by CPlaceId desc
    ) r on p.Id = r.PlaceId
order by r.CPlaceId;

答案 1 :(得分:1)

像这样......

SELECT * FROM PLACE WHERE PLACE.ID IN (
    SELECT PLACEID
    FROM (
        SELECT PLACEID
            , ROW_NUMBER() OVER (ORDER BY CPLACEID DESC) RNUM
        FROM #RESULTPLACELIST
    ) TMP
    WHERE RNUM <= 10
)

答案 2 :(得分:1)

您必须执行JOIN才能完成此操作。它看起来像这样:

SELECT TOP 10 Place.* 
FROM Place
JOIN #ResultPlaceList ON #ResultPlaceList.PlaceId = Place.Id
ORDER BY CPlaceId DESC

答案 3 :(得分:1)

&#39; SELECT C.ID 来自CustomerDetails C. LEFT JOIN UserInfo U ON C.ID = U.ID 由U.ID&#39;

订购

您可以像这样修改您的查询