使用JOIN运算符的Mysql查询

时间:2015-07-10 13:17:38

标签: mysql join

我有两个表: progressdate 项目

表格进度日期

的列
**id      name      progress       enddate**
  127     Rishi     Progress1     10/7/2015
  128     Sahil     Progress2     11/5/2015
  128     Sahil     Progress1     10/5/2015
  127     Rishi     Progress3     1/8/2015
  127     Rishi     Progress2     20/7/2015

项目的列:

**id      progress       file**
  127     Progress1       abc
  128     Progress2       xyz

学生必须将他们的进度文件上传到项目表中。

我想打印进度(即 Progress1 Progress2 ),其中id = 127的学生尚未上传的最低结束日期。

在这种情况下,Rishi (id = 127)尚未上传 Progress2 Progress3 ,其中 Progress2 的结束日期最短,因此输出应为 Progress2

这是我尝试过的:

    SELECT pd.progress,enddate FROM progressdate pd LEFT JOIN project p  
    ON (pd.id=p.id)
    where pd.id="127"  and p.id="127" 
    and enddate = (
        select min(enddate)
        from progressdate pd
        LEFT JOIN project p ON pd.progress=p.progress
        where p.progress IS NULL)

2 个答案:

答案 0 :(得分:0)

这对你有用吗<​​/ p>

select * from progressdate p2
join
(select id, MIN(enddate) edate from progressdate p1
where progress NOT IN (Select Progress FROM project where p1.id=id)
group by id) tmp on p2.id=tmp.id and p2.enddate=tmp.edate

答案 1 :(得分:0)

select DISTINCT p1.progress 
from progressdate p1 LEFT JOIN project p2 
ON p1.id=p2.id
where p1.progress NOT IN (
              Select DISTINCT p1.progress 
              FROM progressdate p
              JOIN project p2 
              where p1.id=p2.id 
              and p1.id="127" 
              and p1.progress=p2.progress
              )
and p1.id="127"
order by enddate desc