繁体   English   中英

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

[英]writing a sql query in MySQL with subquery on the same table

我有一个表svn1:

id | 日期| 开始日期

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。

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

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;

分享和享受。

尝试使用别名,这样的东西应该工作:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM