0

我有一个 SQL 查询(2 个变体),我需要将它与动态查询一起使用。

在 SQL 中看起来是这样(变体 1 // 通过子查询):

SELECT AssetEntry.entryId , (
              SELECT COUNT(*)
              FROM `MBMessage`
              WHERE classPK = AssetEntry.classPK
            ) AS comments
            FROM `AssetEntry`
            ORDER BY comments DESC

或者带有连接和组的替代查询:

SELECT AssetEntry.entryId, count(MBMessage.classPK)
 FROM `AssetEntry`
 JOIN MBMessage ON (AssetEntry.classPK = MBMessage.classPK)
 GROUP BY MBMessage.classPK

两个 SQL 查询显示完全相同!

现在我需要使用其中之一作为动态查询。我不知道如何进行连接,也不知道如何在投影中进行子查询?!

有谁能够帮我?谢谢


我必须用 custom-sql 来做到这一点。

4

2 回答 2

0

Your requirement is a really specific case, which requires an aggregate-function in the SELECT statement.

I would suggest using Custom query (also known as custom-sql in liferay) with finders in your case, instead of a DynamicQuery.

DynamicQuery API has limitations and will not work in your case (speaking from experience, so if somebody else has other opinions or facts regarding the below points I would be more than happy to know):

  1. Joins are not possible with DynamicQuery.
  2. Its possible to return a count or to return an individual columns value using Projection but not possible to return a column and count to-gather using projections.
  3. Can use sub-query with DynamicQuery using projections but I don't think you can use a sub-query in the select statement with DynamicQuery.
于 2012-08-02T11:39:33.067 回答
0

您还有另一种方法,使用 AssetEntryQuery。

AssetEntryQuery aeq = new AssetEntryQuery();
aeq.setClassName(MBMessage.class.getName());
aeq.set.... (Add any other criterions if you want to)
List<AssetEntry> assetEntries = AssetEntryServiceUtil.getEntries(aeq);
于 2012-08-02T15:58:35.777 回答