Apache Torque Criteria:如何加入子查询
假设我有以下 SQL 语句。我想将其转换为扭矩标准:
SELECT table.name, subq1.total AS 'TOTAL', subq2.total2 AS 'SECOND TOTAL'
FROM table
LEFT JOIN (
SELECT c.login, COUNT(*) AS 'total'
FROM table2 INNER JOIN table3
WHERE table3.field = 2
GROUP BY table3.login
) AS subq1 ON(subq1.login = table.login)
LEFT JOIN(...) AS subq2 ON (subq2.login = table.login)
子查询本身并不重要。这里唯一的问题是如何执行 LEFT JOIN。
Let's say I have the follow SQL statement. I would like to convert this to Torque Criteria:
SELECT table.name, subq1.total AS 'TOTAL', subq2.total2 AS 'SECOND TOTAL'
FROM table
LEFT JOIN (
SELECT c.login, COUNT(*) AS 'total'
FROM table2 INNER JOIN table3
WHERE table3.field = 2
GROUP BY table3.login
) AS subq1 ON(subq1.login = table.login)
LEFT JOIN(...) AS subq2 ON (subq2.login = table.login)
It does not matter the subquery itself. The only issue here is how to perform that LEFT JOINs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终将每个子查询拆分为一个单独的方法。
但我也可以使用 Criterion。就像:
所以想法是:每个条件都是一个子查询。你可以输入“或”或“和”或其他任何内容,最后将标准与主要标准结合起来。
I ended up splitting every subquery in a separated method.
But I could also have used Criterion. Something like:
so the idea is: every criterion is a subquery. and you can put "or" or "and" or whatever, and in the end, join the criterion with the main criteria.
在数据库中创建一个新视图来实现复杂的查询,然后创建一个只读 Torque OM 类,您可以从应用程序中进行简单的查询。
Create a new view in the database implementing your complex query, then a read-only Torque OM class you can query trivially from your application.
您可以通过显式定义映射器和选定的列来做到这一点:
考虑
使用扭矩自动映射机制。还有一个 ObjectListMapper..
You could do it by explicitely defining mappers and selected columns:
Consider
using Torque automatically mapping mechanism. There also exists an ObjectListMapper..
基本上,我认为扭矩标准不适合此类查询。
首先,您要选择特定的列。 Criteria 通常用于为查询表选择扭矩对象。不过,您可以使用村庄记录选择特定列,因此实际上可以使用条件选择自定义列,但很麻烦*。
其次,也是最重要的,我不认为 LEFT 连接是可能的。 Criteria 设置为使用 JOIN 基本上作为子查询 AFAIK。
使用基本条件“join”的示例子查询将是:
这将从表 A 中选择条目,其中表 B 列 a = 某个值,表 a 列 b = 表 b 列 b。
总而言之,我只是建议对过于复杂的条件进行直接查询。
Basically, I don't think torque Criteria is suited to this type of query.
First off you're selecting specific columns. Criteria is generally used for selecting torque objects for a queried table. You can select specific columns using village records though, so it is actually possible to select custom column using criteria, but cumbersome*.
Second, and most important, I don't believe a LEFT join is possible. Criteria is set up to use JOINs basically as a subquery AFAIK.
A sample subquery using a basic criteria "join" would be
This would select entries from table A where Table B column a = somevalue and table a column b = table b column b.
All in all I would just recommend a direct query for overly complex criteria.