带参数的CRUDRepository本机查询

时间:2018-06-26 11:38:17

标签: spring-boot jpa spring-data-jpa spring-data

我在Spring数据JpaRepository中使用本地查询,如下所示:

  @Query(value = "SELECT SUBSTRING_INDEX(u.email, '@', -1) as domain, COUNT(*) as domainCount r.invite_organization_id"
        + "  FROM srs_users u,srs.srs_user_registrations r where u.user_id=r.user_id and r.invite_organization_id=?1"
        + "  GROUP BY "
        + "SUBSTRING_INDEX(u.email, '@', -1) ORDER BY domainCount DESC", nativeQuery = true)
List<Object[]> countTopDomain(String orgId);

以上内容为我提供了以下异常:    您的SQL语法有误;在第1行的'r.invite_organization_id FROM srs_users u,srs_user_registrations r where u.user'附近,查看与您的MySQL服务器版本相对应的手册以使用正确的语法

我该如何从方法countTopDomain()的参数中传递Invitation_organization_id(在查询中)的值。

2 个答案:

答案 0 :(得分:1)

'r.invite_organization_id FROM srs_users u,srs_user_registrations r',语法错误。计数(*)为domainCount后缺少','

答案 1 :(得分:0)

尝试这个

@Query(value = "SELECT SUBSTRING_INDEX(u.email, '@', -1) as domain, COUNT(*) as domainCount, r.invite_organization_id"
    + "  FROM srs_users u,srs_user_registrations r where u.user_id=r.user_id and r.invite_organization_id=?1"
    + "  GROUP BY "
    + "SUBSTRING_INDEX(u.email, '@', -1) ORDER BY domainCount DESC", nativeQuery = true)
List<Object[]> countTopDomain(String orgId);