简体   繁体   中英

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

I have a table svn1:

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);

Now the problem is how do I let know the subquery to match id with the id in the outer query. Obviously id=svn1.id wont work. 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.

Can I just calculate this once and store it using AS and then use that variable later. Main problem is to correct id=svn1.id so as to correctly equate it to the id in the outer table.

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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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