请求日期和加入JPA

时间:2016-03-14 10:57:41

标签: java hibernate jpa

我有两张桌子:男人和入口。

[man]

+------+-------+
| id   | name  |
+------+-------+
| 1    | man1  |
| 2    | man2  |
+------+-------+ 

[Entrance]

+------+--------------------------+-------+
| id   |         date             | idman |
+------+--------------------------+-------+
| 1    | 2008-10-29 14:56:59      |   1   |
| 2    | 2008-10-29 14:56:59      |   2   |
| 3    | 2008-10-29 17:56:59      |   2   |
| 4    | 2008-10-30 14:56:59      |   2   |
+------+--------------------------+-------+ 

[result]


+----------------+-------+
| count entrance | name  |
+----------------+-------+
| 1              | man1  |
| 2              | man2  |
+----------------+-------+ 

man2超过2,因为2个入口在同一天。 你能帮我倒一下Requette jpa吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是你期待的吗?

SELECT max(t1.count_by_id), m.name, FROM (  
   SELECT id, cast(date as date) as cdate, count(*) as count_by_id
   FROM entrance    
   GROUP BY cdate, id) as t1 
INNER JOIN man m ON (t1.id = m.id) 
GROUP BY t1.id;