【发布时间】:2018-02-16 14:35:38
【问题描述】:
我正在尝试将 sql 查询传递给 laravel,但在此查询中,表是使用复合主键连接的,此外它还有这些表的子查询,所以我不知道该怎么做
sql代码如下图
select distinct g.id,
(
select count(*) from xxx s(nolock) inner join yyy t(nolock)
on t.id=s.id
and t.year=s.year
and t.code=s.code
where s.year = 2017
and t.status<>'C'
and s.created_at>=convert(datetime,'16/08/2017 00:00:00',103)
and s.created_at<=convert(datetime,'22/08/2017 23:59:59',103)
and s.id=g.id
) as xxx,
(
select count(*) from zzz s(nolock) inner join yyy t(nolock)
on t.id=s.id
and t.year=s.year
and t.code=s.code
where s.year = 2017
and t.status<>'C'
and s.created_at>=convert(datetime,'16/08/2017 00:00:00',103)
and s.created_at<=convert(datetime,'22/08/2017 23:59:59',103)
and s.id=g.id
) as zzz
from globals g(nolock) order by g.id
它有很多像我放在这里的这两个子查询,连接中的相同条件和相同的 where 子句但不同的表,知道如何在 laravel 中执行此操作吗?
【问题讨论】: