我在我的应用程序中使用queryDsl进行复杂的搜索查询。我是querydsl的新手。我从下面的代码开始,从一个表(TableA)中获取几行。
但是我必须在其他表(TableB)中找到具有相同ID的人员列表(计数)

public static Predicate find(final Long pId)
    QPerson qPerson = QPerson.person;
    Predicate predicate;
    BooleanBuilder booleanBuilder = new BooleanBuilder();
    if (pId != null) {
        booleanBuilder.or(qPerson.person_no.eq(pId));
    }
    if (name != null && !name.isEmpty()) {
        booleanBuilder.or(qPerson.expiry_dt.eq(name));
    }
    predicate = booleanBuilder.getValue();
    return predicate;
    }


表A:

pId         name

1001      sampleNameA
1002      sampleNameB
1003      sampleNameC


表B:

pId        name       interests

1001     sampleNameA    music
1001     sampleNameA    dance
1001     sampleNameA    coding
1003     sampleNameC    music
1002     sampleNameB    dance
1002     sampleNameB    coding


我需要通过以下查询获取类似的输出

从master_person_table tableA中选择cnt cnt,tableA。*(从tableB中选择count(*)cnt WHERE pId ='1002')cnt
pId ='1002'

输出:

  count  pId        name
    2    1002   sampleNameB


我需要在HTML中显示行号(对于id = 1002)。

谁能帮我找到要从tableB中获取的pId的计数吗?

提前致谢

最佳答案

请尝试以下查询:-

1.对于多个pId:-

SELECT COUNT(*) AS COUNT, pId, name FROM TableB GROUP BY pId


2.仅适用于选定的pId:-

SELECT COUNT(*) AS COUNT, pId, name FROM TableB WHERE pId = 1002


如果您需要更多帮助,请告诉我。

10-04 11:32
查看更多