2

是否可以在不在投影本身中的字段上对 SQL 查询的结果进行排序?

请参见下面的示例 - 我正在使用产品表的不同 ID,但我希望它按标题排序。我不想包含标题,因为我正在使用 NHibernate 生成查询,并对结果进行分页。然后我使用这个不同的 ID 结果集来加载实际结果。

SELECT
DISTINCT this_.`ID` AS y0 
FROM
`Product` this_ 
LEFT OUTER JOIN
`Brand` brand3_ 
ON this_.BrandId=brand3_.ID 
INNER JOIN
`Product_CultureInfo` productcul2_ 
ON this_.ID=productcul2_.ProductID 
AND (
(
(
productcul2_.`Deleted` = 0 
OR productcul2_.`Deleted` IS NULL
) 
AND (
productcul2_.`_Temporary_Flag` = 0 
OR productcul2_.`_Temporary_Flag` IS NULL
)
)
) 
INNER JOIN
`ProductCategory` aliasprodu1_ 
ON this_.ID=aliasprodu1_.ProductID 
AND (
(
(
aliasprodu1_.`Deleted` = 0 
OR aliasprodu1_.`Deleted` IS NULL
) 
AND (
aliasprodu1_.`_Temporary_Flag` = 0 
OR aliasprodu1_.`_Temporary_Flag` IS NULL
)
)
) 
WHERE
(
this_._Temporary_Flag =FALSE
OR this_._Temporary_Flag IS NULL
) 
AND this_.Published = TRUE
AND (
this_.Deleted = FALSE
OR this_.Deleted IS NULL
) 
AND (
this_._ComputedDeletedValue = FALSE
OR this_._ComputedDeletedValue IS NULL
) 
AND (
(
this_._TestItemSessionGuid IS NULL 
OR this_._TestItemSessionGuid = ''
)
) 
AND (
productcul2_._ActualTitle LIKE '%silver%' 
OR brand3_.Title LIKE '%silver%' 
OR aliasprodu1_.CategoryId IN (
47906817 , 47906818 , 47906819 , 47906816 , 7012353 , 44662785 
)
) 
AND this_.Published = TRUE
AND this_.Published = TRUE
ORDER BY
this_.Priority ASC,
productcul2_._ActualTitle ASC,
this_.Priority ASC LIMIT 25;
4

1 回答 1

1

不知道是否有更好的解决方案,但是如果外部查询排除您不感兴趣的字段的嵌套选择呢?

所以,在“随机”桌子上的东西

SELECT a,b,c from (SELECT a,b,c,d from myTable order by d)

显然,如果有一个“语言直接”的解决方案会更好,因为那样的话,你必须做两个投影,其中一个是没用的

于 2012-05-28T15:21:52.760 回答