除了具体的例子,我几乎不知道如何提出这个问题,所以这里是:
我们有一个活动注册表,我希望匹配在过去5年中每年注册过4个活动之一的注册人。
我能想到这样做的唯一方法是使用冗长的子查询,但在性能方面它是绝对的狗:
<div class="layout-header">
<h1></h1>
</div>
<div class="layout-middle">
<div class="layout-left">
<!-- start content -->
<div class="panel">
<header>panel header</header>
<div class="select-list" id="select-list">
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
</div>
<footer></footer>
</div>
<div class="panel">
<header>Entities</header>
<div class="select-list" id="select-list">
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
</div>
<footer></footer>
</div>
<div class="panel">
<header>Entities</header>
<div class="select-list" id="select-list">
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
<div class="select-list-item selected">0: bg</div>
</div>
<footer></footer>
</div>
<!-- end content -->
</div>
<main class="layout-stage">
<h2>stage</h2>
</main>
<aside class="layout-right">
<h2>right</h2>
</aside>
</div>
<div class="layout-footer">
<h2>Footer</h2>
</div>
每年等等。就像我说的那样,不是很有说服力或效率很高。
有更简单的方法吗?
答案 0 :(得分:1)
您可以使用GROUP BY
执行HAVING
,然后使用当前年度事件执行INTERSECT
SELECT FirstName, LastName, DOB
FROM RegTable
WHERE EventId IN (1,2,3,4)
AND EventYear IN (2011,2010,2009,2008,2007)
GROUP BY FirstName, LastName, DOB
HAVING COUNT(Distinct EventYear) = 5
INTERSECT
SELECT DISTINCT FirstName ,LastName ,DOB
FROM RegTable
WHERE EventId IN (1,2,3,4)
AND EventYear = 2012
以上查询与样本数据一起使用。 SQL Fiddle
答案 1 :(得分:0)
我最好不明白这个问题,只是重写你的查询(假设它正在做你需要的):
SELECT RT2011.FirstName, RT2011.LastName, RT2011.EmailAddress
FROM
RegTable RT2011,
RegTable RT2012
WHERE RT2011.EventId IN (1,2,3,4)
AND RT2012.EventId IN (1,2,3,4)
AND RT2011.EventYear = 2011
AND RT2012.EventYear = 2012
AND RT2011.FirstName = RT2012.FirstName
AND RT2011.LastName = RT2012.LastName
AND RT2011.DOB = RT2012.DOB