我有一個表svn1:在同一表上寫的SQL查詢在MySQL中使用子查詢
id | date | startdate
23 2002-12-04 2000-11-11
23 2004-08-19 2005-09-10
23 2002-09-09 2004-08-23
select id,startdate from svn1 where startdate>=(select max(date) from svn1 where id=svn1.id);
現在的問題是我怎麼讓知道子查詢中與ID匹配ID在外部查詢中。顯然id = svn1.id不會工作。謝謝!
If you have the time to read more:
這真的是一個簡單的問題,問我真的想在這裏做什麼。我實際的查詢是這樣的
select
id, count(distinct archdetails.compname)
from
svn1,svn3,archdetails
where
svn1.name='ant'
and svn3.name='ant'
and archdetails.name='ant'
and type='Bug'
and svn1.revno=svn3.revno
and svn3.compname=archdetails.compname
and
(
(startdate>=sdate and startdate<=edate)
or
(
sdate<=(select max(date) from svn1 where type='Bug' and id=svn1.id)
and
edate>=(select max(date) from svn1 where type='Bug' and id=svn1.id)
)
or
(
sdate>=startdate
and
edate<=(select max(date) from svn1 where type='Bug' and id=svn1.id)
)
)
group by id LIMIT 0,40;
正如你看到select max(date) from svn1 where type='Bug' and id=svn1.id
,必須計算多次。
我可以只計算一次並使用AS
存儲它,然後再使用該變量。主要問題是糾正id=svn1.id
,以便將它正確地等同於外表中的id。
非常感謝。這正是我所期待的。但是這實際上增加了執行時間。雖然我沒有桌上的索引。通過查看查詢,什麼是創建索引的最佳列?或者使用其他方法? – Gaurav 2010-10-20 15:47:50
@Gaurav:請將這些表格的定義添加到您的原始問題中。如果不理解表格的內容,很難提出建議。謝謝。 – 2010-10-20 16:00:46