将复杂查询存储为命名查询

时间:2013-04-15 04:56:13

标签: mysql sql named-query

我正在使用JSF 2做一个项目,我有一个特别复杂的查询来做电影建议。我试图把它作为一个命名查询,编译器告诉我有一个错误,老实说我不知道​​在哪里看

SELECT M.*
FROM Account A,
     Movie M,
     Orders O,
     Rental R,
  (SELECT Acc.Id
   FROM Account Acc,
     (SELECT COUNT(*) AS c,
             Acc.Id
      FROM Account Acc,
        (SELECT M.*
         FROM Account A,
              Rental R,
              Orders O,
              Movie M
         WHERE A.Id=R.AccountId
           AND R.MovieId=M.Id
           AND R.OrderId=O.Id
           AND A.Id=:id) AS a
      INNER JOIN
        (SELECT M.*,
                A.Id AS AccountId
         FROM Account A,
              Rental R,
              Orders O,
              Movie M
         WHERE A.Id=R.AccountId
           AND R.MovieId=M.Id
           AND R.OrderId=O.Id
           AND A.Id<>:id) AS b USING (Id,Name,TYPE,Rating,DistrFee,NumCopies)
      WHERE Acc.Id = b.AccountId ) AS COUNT
   WHERE COUNT.c>0
     AND COUNT.Id = Acc.Id) AS SimilarAccount
WHERE A.Id=R.AccountId
  AND R.MovieId=M.Id
  AND R.OrderId=O.Id
  AND A.Id=:id
  AND M.Id NOT IN
    (SELECT M.Id
     FROM Account A,
          Rental R,
          Orders O,
          Movie M
     WHERE A.Id=R.AccountId
       AND R.MovieId=M.Id
       AND R.OrderId=O.Id
       AND A.Id=SimilarAccount.Id)

除了性能问题(教授希望通过MySQL完成电影建议),我不确定存储此查询以重复使用的过程。我会将查询存储为视图,但它有一个参数(它需要用户的参数ID)

您如何建议存储此查询?

1 个答案:

答案 0 :(得分:0)

我建议创建一个带有键值对的Java Properties文件,并在DAO类中创建一个Properties类型的私有字段,并使用Spring配置将其注入,该配置将从文件中读取值,或者您可以考虑使用存储过程。如果我理解你的话......