我有 2 个表(AllClients
和 AllActivities
),需要检索以下信息:
我需要一个不同客户的列表,其中最近的事件已在去年输入。我已经使用了以下代码,但速度非常慢,因此没有用。我相信加入(没有子查询)会更快,但我就是想不通。这是我当前的 sql 语句:
select distinct(AllClients.LookupCode)
from AllClients
join (select LookupCode,
max(AllActivities.EnteredDate) as EnteredDate
from AllActivities
group by LookupCode) AllActivities
on AllClients.LookupCode = AllActivities.LookupCode
where AllClients.Name = '$userName'
and AllClients.TypeCode = 'P' and AllActivities.EnteredDate < '$oneYearAgo'";
最佳答案
试试这个:
select AllClients.LookupCode
from AllClients
join AllActivities on AllClients.LookupCode = AllActivities.LookupCode
where AllClients.Name = '$userName' and AllClients.TypeCode = 'P'
group by AllClients.LookupCode
having max(AllActivities.EnteredDate) < '$oneYearAgo';
关于mysql - 需要帮助提高 SQL 性能(子查询与连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19278085/