将SQL查询转换为JPQL

时间:2011-07-06 13:08:18

标签: mysql jpql

以下查询:

SELECT kinder.kindID, kinder.kindName, kinder.kindVorname 
FROM kinder
INNER JOIN (SELECT kindName FROM kinder
    GROUP BY kindName HAVING count(kindID) > 1) dup ON kinder.kindName = dup.kindName
ORDER BY kindName

从我的MySQL数据库中查找并显示重复的候选人 现在我需要帮助将其翻译成JPQL,到目前为止还没有成功。

2 个答案:

答案 0 :(得分:1)

我刚刚进行了实验,提出了与JB相同的解决方案。这在WebSphere上的OpenJPA中运行。

    SELECT k.id, k.name, k.vorname from Kinder k where 
        k.name IN (
        SELECT dup.name FROM Kinder dup 
             GROUP BY dup.name  HAVING count(dup.id) > 1)   

答案 1 :(得分:0)

我不确定JPQL中允许哪些子查询,但您的查询:

SELECT kinder.kindID, kinder.kindName, kinder.kindVorname 
FROM kinder
  INNER JOIN
    ( SELECT kindName
      FROM kinder
      GROUP BY kindName
      HAVING count(kindID) > 1
    ) dup
    ON kinder.kindName = dup.kindName
ORDER BY kinder.kindName

也可以使用IN编写(EXISTS方式除外):

SELECT k.kindID, k.kindName, k.kindVorname 
FROM kinder k
WHERE EXISTS
      ( SELECT *
        FROM kinder k2
        WHERE k2.kindName = k.kindName
          AND k2.kindID <> k.kindID
      ) 
ORDER BY k.kindName

或使用ANY

SELECT k.kindID, k.kindName, k.kindVorname 
FROM kinder k
WHERE k.kindID <> ANY
      ( SELECT k2.kindID
        FROM kinder k2
        WHERE k2.kindName = k.kindName
      ) 
ORDER BY k.kindName