[英]writing a sql query in MySQL with subquery on the same table
I have a table svn1: 我有一个表svn1:
id |
id | date |
日期| startdate
开始日期
23 2002-12-04 2000-11-11
23 2002-12-04 2000-11-11
23 2004-08-19 2005-09-1023 2004-08-19 2005-09-10
23 2002-09-09 2004-08-2323 2002-09-09 2004-08-23
select id,startdate from svn1 where startdate>=(select max(date) from svn1 where id=svn1.id);
Now the problem is how do I let know the subquery to match id with the id in the outer query. 现在问题是如何让子查询知道id与外部查询中的id匹配。 Obviously id=svn1.id wont work.
显然id = svn1.id不会工作。 Thanks!
谢谢!
If you have the time to read more:
如果您有时间阅读更多内容:
This really is a simplified version of asking what I really am trying to do here. 这真的是一个简化版本,询问我在这里想要做什么。 my actual query is something like this
我的实际查询是这样的
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;
As you notice select max(date) from svn1 where type='Bug' and id=svn1.id
has to be calculated many times. 当您注意到
select max(date) from svn1 where type='Bug' and id=svn1.id
必须多次计算。
Can I just calculate this once and store it using AS
and then use that variable later. 我可以只计算一次并使用
AS
存储它,然后再使用该变量。 Main problem is to correct id=svn1.id
so as to correctly equate it to the id in the outer table. 主要问题是纠正
id=svn1.id
,以便正确地将它等同于外表中的id。
I'm not sure you can eliminate the repetition of the subquery, but the subquery can reference the main query if you use a table alias, as in the following: 我不确定您是否可以消除子查询的重复,但如果使用表别名,子查询可以引用主查询,如下所示:
select id,
count(distinct archdetails.compname)
from svn1 s1,
svn3 s3,
archdetails a
where s1.name='ant' and
s3.name='ant' and
a.name='ant' and
type='Bug' and
s1.revno=s3.revno and
s3.compname = a.compname and
( (startdate >= sdate and startdate<=edate) or
(sdate <= (select max(date)
from svn1
where type='Bug' and
id=s1.id and
edate>=(select max(date)
from svn1
where type='Bug' and
id=s1.id)) or
(sdate >= startdate and edate<=(select max(date)
from svn1
where type='Bug' and
id=s1.id)) )
group by id LIMIT 0,40;
Share and enjoy. 分享和享受。
尝试使用别名,这样的东西应该工作:
select s.id,s.startdate from svn1.s where s.startdate>=(select max(date) from svn1.s2 where s.id=s2.id);
You should be able to left join to a sub-select so you only run the query once. 您应该能够将联接保留为子选择,因此您只需运行一次查询。 Then you can do a join condition to pull out the maximum for the ID on each record as shown below:
然后,您可以执行连接条件,以获取每条记录上ID的最大值,如下所示:
SELECT id,
COUNT(DISTINCT archdetails.compname)
FROM svn1,
svn3,
archdetails
LEFT JOIN (
SELECT id, MAX(date) AS MaximumDate
FROM svn1
WHERE TYPE = 'Bug'
GROUP BY id
) AS MaxDate ON MaxDate.id = svn1.id
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 <= MaxDate.MaximumDate
AND edate >= MaxDate.MaximumDate
)
OR (
sdate >= startdate
AND edate <= MaxDate.MaximumDate
)
)
GROUP BY
id LIMIT 0,
40;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.