我有一个assigns
表,其中包含以下列:
id - int
id_lead - int
id_source - int
date_assigned - int (this represents a unix timestamp)
现在,假设我在此表中有以下数据:
id id_lead id_source date_assigned
1 20 5 1462544612
2 20 6 1462544624
3 22 6 1462544615
4 22 5 1462544626
5 22 7 1462544632
6 25 6 1462544614
7 25 8 1462544621
现在,假设我想得到id_source为6的行的计数,并且是每个潜在客户的第一个条目(按date_assigned asc排序)。
所以在这种情况下,count会= 2,因为有2个引导(id_lead 22和25),其第一个id_source是6。
我如何编写此查询以使其快速并且可以作为子查询选择正常工作?我在想这样的事情是行不通的:
select count(*) from `assigns` where `id_source`=6 order by `date_assigned` asc limit 1
我不知道如何以最佳方式编写此查询。任何帮助将不胜感激。
答案 0 :(得分:0)
简单查询
点击http://sqlfiddle.com/#!9/8666e0/7
select count(*) from
(select * from assigns group by id_lead )t
where t.id_source=6
答案 1 :(得分:0)
伪代码:
select rows
with a.id_source = 6
but only if
there do not exist any row
with same id_lead
and smaller date_assigned
将其翻译为SQL
select * -- select rows
from assigns a
where a.id_source = 6 -- with a.id_source = 6
and not exists ( -- but only if there do not exist any row
select 1
from assigns a1
where a1.id_lead = a.id_lead -- with same id_lead
and a1.date_assigned < a.date_assigned -- and smaller date_assigned
)
现在将select *
替换为select count(*)
,您就会得到结果。
http://sqlfiddle.com/#!9/3dc0f5/7
<强>更新强>
可以将NOT-EXIST查询重写为排除LEFT JOIN查询:
select count(*)
from assigns a
left join assigns a1
on a1.id_lead = a.id_lead
and a1.date_assigned < a.date_assigned
where a.id_source = 6
and a1.id_lead is null
如果您想获取id_source
的所有值的计数,则下面的查询可能是最快的:
select a.id_source, count(1)
from (
select a1.id_lead, min(a1.date_assigned) date_assigned
from assigns a1
group by a1.id_lead
) a1
join assigns a
on a.id_lead = a1.id_lead
and a.date_assigned = a1.date_assigned
group by a.id_source
您仍然可以将group by a.id_source
替换为where a.id_source = 6
。
查询需要assigns(id_source)
和assigns(id_lead, date_assigned)
上的索引。