父子关系: 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名员工。
发布于 2020-07-29 01:52:43
您可以使用可分页,它可以获取知道父实体的子实体。请注意,此查询将返回Child的分页结果。
/**
* Find Child entities knowing the Parent.
*/
@Query("select child from Parent p inner join p.childs child where p = :parent")
public Page<Child> findBy(@Param("parent") Parent parent, Pageable pageable);你可以这样使用它:
Page<Child> findBy = repo.findBy(parent, new PageRequest(page, size));https://stackoverflow.com/questions/63139782
复制相似问题