Hibernate从多个表中计数 [英] Hibernate count from multi tables
问题描述
我有4个表:
Tab2
, Tab3
和 Tab4
,我想得到这样的东西: | Tab1 | Tab2 | Tab3 | Tab4 |
| .1 .... | ..13 ... | ... 7 ... | .... 0 ... |
Tab#
中的所有记录都有它们的记录自己的 id
和 ObjectID
和 ObjectFetch
列
我试过这样的事情:
select DISTINCT
(select计数(*)从Tab1其中ObjectFetch =:fetch和ObjectID =:id),
(从Tab2中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id),
(select count *)from Tab3其中ObjectFetch =:fetch和ObjectID =:id),
(从Tab4中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id),
来自Tab1,Tab2,Tab3 ,Tab4
但是它根本不起作用,为什么呢?
我找到了一个解决方案,根本就不需要在 FROM
之后使用所有表格,我们可以只使用1,它只是一个正确的语法,我们也可以使用一个virtualTable,所以正确的HQL
选择DISTINCT
(从Object Tab中的Tab1中选择count(*) (从Tab2中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id),
(从Tab3中选择count(*),其中ObjectFetch = :fetch和ObjectID =:id),
(从Tab4中选择count(*),其中ObjectFetch =:fetch和ObjectID =:id)
from Tab1
及其全部OK :) thx。
I would like to write a single HQL query that return a specific count from 4 tables in different columns.
I have 4 tables: Tab1
, Tab2
, Tab3
and Tab4
, and I would like to get something like this:
|Tab1 | Tab2 | Tab3 | Tab4 |
|..1....|..13...|...7...|....0...|
All records in Tab#
s have their own id
and ObjectID
and ObjectFetch
columns
I have tried something like this:
select DISTINCT
(select count(*) from Tab1 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab2 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab3 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab4 where ObjectFetch=:fetch and ObjectID=:id),
from Tab1, Tab2, Tab3, Tab4
But it doesn't work at all, any idea why?
I found a solution, simply there is no need to use all tables after FROM
we can use only 1, it is just for a correct syntax, also we can use a virtualTable, so the right HQL
select DISTINCT
(select count(*) from Tab1 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab2 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab3 where ObjectFetch=:fetch and ObjectID=:id),
(select count(*) from Tab4 where ObjectFetch=:fetch and ObjectID=:id)
from Tab1
and its all ok :) thx.
这篇关于Hibernate从多个表中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!