Doctrine 2: QueryBuilder Join with subquery
Doctrine 2: QueryBuilder Join with subquery
我需要将本机查询转换为学说查询。
本机查询
select u.*, v.browser_id
from users u
left join visits v
on u.id = v.user_id
and v.id = (
select max(id) from visits v2
where v2.user_id = u.id
)
你可以在这里看到,我需要 select 使用最新浏览器访问的用户
所以要将此查询转换为 DQL,我明白我需要做这样的事情
$queryBuilder->leftJoin('u.visits', 'lastVis', Join::WITH,
// ????
);
但我不知道具体是什么。也许有人会很快解决这个问题?
谢谢,看来我找到了解决办法:)
$subQuery = $this->entityManager->createQueryBuilder()
->select('MAX(vis2.id)')
->from(Visit::class, 'vis2')
->where('vis2.user = u.id');
$this->entityManager->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->leftJoin(
'u.visits', 'vis',
Join::WITH, sprintf('vis.id = (%s)', $subQuery->getDQL()),
);
我需要将本机查询转换为学说查询。
本机查询
select u.*, v.browser_id
from users u
left join visits v
on u.id = v.user_id
and v.id = (
select max(id) from visits v2
where v2.user_id = u.id
)
你可以在这里看到,我需要 select 使用最新浏览器访问的用户
所以要将此查询转换为 DQL,我明白我需要做这样的事情
$queryBuilder->leftJoin('u.visits', 'lastVis', Join::WITH,
// ????
);
但我不知道具体是什么。也许有人会很快解决这个问题?
谢谢,看来我找到了解决办法:)
$subQuery = $this->entityManager->createQueryBuilder()
->select('MAX(vis2.id)')
->from(Visit::class, 'vis2')
->where('vis2.user = u.id');
$this->entityManager->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->leftJoin(
'u.visits', 'vis',
Join::WITH, sprintf('vis.id = (%s)', $subQuery->getDQL()),
);