【发布时间】:2020-11-18 05:28:16
【问题描述】:
父子关系:OneToMany
Parent - table
+-----+---------+
|id | name |
+-----+---------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
+---------------+
child1 - table
+--+---------+------+
|id|parent_id|name |
+--+---------+------+
| 1| 1 | qwe |
| 2| 1 | asd |
| 3| 1 | dsf |
| 4| 2 | xzc |
+--+---------+------+
表:公司(父)和雇员(子)。我想获取所有给定公司的 100 名最近加入的员工以及公司的详细信息。
SELECT c.*, e.*
FROM company c
JOIN (
SELECT *
FROM employee
WHERE company_id IN(2,3,4,5)
ORDER BY joined_at DESC LIMIT 100
) e on e.company_id = c.id where c.id in (2,3,4,5)
上面的查询一共只返回了 100 名员工,但我希望 IN 子句中提供的每个公司都有 100 名员工。
【问题讨论】:
-
问题是?
-
@Andronicus 我已经更新了问题。
-
每家公司有3名员工,见meta.stackoverflow.com/questions/333952/…
标签: mysql hibernate jdbc one-to-many hibernate-mapping