Cakephp版本:3.5。
我有一个索赔表。一个项目有很多主张。索赔有一个项目,一个数字(1、2、3和运行中的数字),每个数字可以多次修改。 project_id,number和rev_number的组合构成唯一声明。表格看起来像这样(在右边,我已经标记了查询中想要的行):
TextSearch.TextPath
所以我想为每个索偿号(project_id = 1)进行最新修订(具有最高rev_number)。
我想用ORM生成的sql就像
DisplayMemberPath
如何用ORM生成此sql? +----+------------+--------+------------+---------+
| id | project_id | number | rev_number | i want |
+----+------------+--------+------------+---------+
| 1 | 2 | 1 | 1 | |
+----+------------+--------+------------+---------+
| 2 | 1 | 1 | 1 | <- this |
+----+------------+--------+------------+---------+
| 3 | 1 | 2 | 1 | |
+----+------------+--------+------------+---------+
| 4 | 1 | 3 | 1 | |
+----+------------+--------+------------+---------+
| 5 | 1 | 2 | 2 | |
+----+------------+--------+------------+---------+
| 6 | 1 | 2 | 3 | <- this |
+----+------------+--------+------------+---------+
| 7 | 1 | 3 | 2 | <- this |
+----+------------+--------+------------+---------+
是如何产生的?在调查之后,我找不到使用join(),innerJoin(),innerJoinWith()或matching()的解决方案。
答案 0 :(得分:0)
如@ndm所评论,通过将join()
或innerJoin()
与queryObject((sub)query)一起使用,
$subq = $this->Claims->find()
->select(['number' => 'Claims.number', 'latest' => 'MAX(Claims.rev_number)'])
->where(['project_id' => $pid])
->group(['number']);
AND
$query = $this->Claims->find()
->contain(['ClaimRows'])
->join([
'GroupedClaims' => [
'table' => $subq,
'type' => 'INNER',
'conditions' => ['GroupedClaims.number = Claims.number', 'GroupedClaims.latest = Claims.rev_number']
]
])
->all();
OR
$query = $this->Claims->find()
->contain(['ClaimRows'])
->innerJoin(
['GroupedClaims' => $subq],
['GroupedClaims.number = Claims.number', 'GroupedClaims.latest = Claims.rev_number'])
->all();