使用QueryDSL JPA在MySQL中查找重复的行

时间:2012-10-09 16:33:25

标签: java mysql jpa querydsl

我想在QueryDSL JPA中执行此SQL请求

SELECT authorizationitem.* 
FROM authorizationitem 
INNER JOIN 
(
    SELECT `authorize` 
    FROM authorizationitem 
    GROUP BY `authorize` 
    HAVING COUNT(*)>1
) a2 
    ON authorizationitem.`authorize` = a2.`authorize`;

为了在表中找到重复的行,我应该执行此请求。但是使用QueryDSL,我找不到写这个的方法。

似乎QueryDSL不允许在Inner Join中使用subQuery:s

有什么建议吗?

由于 的问候,

2 个答案:

答案 0 :(得分:1)

你无法用HQL / JPQL表达这一点,所以你需要用SQL来表达它。 Querydsl JPA提供了通过其API表达JPQL和SQL查询的可能性。

答案 1 :(得分:0)

您可以尝试在WHERE子句中使用子查询,但它可能不如FROM子句中的子查询有效。确保在authorizationitem.authorize上有一个索引来优化连接和GROUP BY。

SELECT authorizationitem.* 
FROM authorizationitem 
WHERE EXISTS (
    SELECT `authorize` 
    FROM authorizationitem2 
   WHERE authorizationitem2.authorize = authorizationitem.authorize
    GROUP BY `authorize` 
    HAVING COUNT(*)>1
);

SELECT authorizationitem.* 
FROM authorizationitem 
WHERE (
    SELECT count(*)
    FROM authorizationitem2 
   WHERE authorizationitem2.authorize = authorizationitem.authorize
    GROUP BY `authorize` 
) > 1;