在MySQL中使用子查询在同一个表上编写SQL查询

时间:2010-10-20 15:16:45

标签: sql mysql

我有一张桌子svn1:

  

id |日期| 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不会工作。谢谢!

  

如果您有时间阅读更多内容:

这真的是一个简化的版本,询问我在这里尝试做什么。我的实际查询是这样的

    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。

3 个答案:

答案 0 :(得分:1)

我不确定您是否可以消除子查询的重复,但如果您使用表别名,子查询可以引用主查询,如下所示:

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;

分享并享受。

答案 1 :(得分:0)

尝试使用别名,这样的事情应该有效:

select s.id,s.startdate from svn1.s where s.startdate>=(select max(date) from svn1.s2 where s.id=s2.id);

答案 2 :(得分:0)

您应该可以将联接保留为子选择,这样您只需运行一次查询。然后,您可以执行连接条件,以获取每条记录上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;