6

这是详细模式下的问题。我在认证表上有认证实例,它们都有一个类型和一个与之关联的学生。这就是我想要发生的事情,我想提取所有到期日期范围内的证书(现在到 1 年)。如果他们符合该参数的条件,那很好,但是如果他们的证书到期时间大于范围,当它与该范围内的证书类型相同时,我想排除学生——他们不需要在那个报告。这是我的第一个查询:

SELECT s.student_id, s.fname, s.address1, s.lname, s.zip, s.state, s.city, s.student_type
          FROM students s, certifications c
          WHERE ( s.student_id = c.student_id )
            AND s.status='A' 
            AND s.student_type != 'B' 
            AND s.student_type != 'D'  
            AND s.student_type != 'E'
            AND s.student_type != 'W'  
            AND s.student_type != 'T'  
            AND s.student_type != 'I'  
            AND c.expiration >= CURDATE() AND c.expiration <= DATE_ADD(NOW(), INTERVAL 1 YEAR)
          GROUP BY s.student_id
             ORDER BY s.lname, s.fname

那么实际得到的sql就是根据前面的sql语句得到的证书信息:

SELECT c.cert_num, c.date, expiration, ct.name, ct.cert_type, c.cert_id, c.student_id
              FROM  certifications c, cert_type ct 
              WHERE student_id = '{$Row['student_id']}'
              AND ct.cert_type = c.cert_type
              ORDER BY ct.name ASC, expiration DESC

总而言之,我遇到的问题是,如果他们的证书到期时间在一年内,并且如果他们有另一个相同类型的证书,其到期时间大于该范围,学生就会出现。那不好。

有没有办法检查以确保某个证书类型在日期范围内,如果是,那么确保他们没有大于该范围的相同类型的证书?它不关心是否需要另一个 sql 查询。

4

3 回答 3

1

有可能我过度简化了问题,但你能不能只获得任何学生/类型组合的最长到期日期?例如

SELECT  s.student_id, 
        s.fname, 
        s.address1, 
        s.lname, 
        s.zip, 
        s.state, 
        s.city, 
        s.student_type
FROM    Students s
        INNER JOIN 
        (   SELECT  c.student_ID, 
                    c.Cert_Type, 
                    MAX(Expiration) AS Expiration
            FROM    Certifications c
            GROUP BY c.student_ID, c.Cert_Type
        ) c
            ON s.Student_ID = c.Student_ID
WHERE   s.Student_Type NOT IN ('B', 'D', 'E', 'W', 'T', 'I')
AND     c.expiration >= CURDATE() AND c.expiration <= DATE_ADD(NOW(), INTERVAL 1 YEAR)
GROUP BY s.student_id, s.fname, s.address1, s.lname, s.zip, s.state, s.city, s.student_type
于 2012-10-29T17:24:24.697 回答
1

使用子查询:

SELECT s.student_id, s.fname, s.address1, s.lname, s.zip, s.state, s.city, s.student_type
  FROM students s, certifications c
  WHERE ( s.student_id = c.student_id )
    AND s.status='A' 
    AND s.student_type != 'B' 
    AND s.student_type != 'D'  
    AND s.student_type != 'E'
    AND s.student_type != 'W'  
    AND s.student_type != 'T'  
    AND s.student_type != 'I'  
    AND c.expiration >= CURDATE() AND c.expiration <= DATE_ADD(NOW(), INTERVAL 1 YEAR)
    AND NOT EXISTS (
      SELECT c_inner.id 
      FROM certifications c_inner 
      WHERE c.expiration > DATE_ADD(NOW(), INTERVAL 1 YEAR)
        AND c.student_id = c_inner.student_id
    )
  GROUP BY s.student_id
    ORDER BY s.lname, s.fname

我认为应该可以将其重构为连接,这样可以提供更好的性能。

于 2012-10-29T17:29:33.777 回答
0
SELECT s.student_id, s.fname, s.address1, s.lname, s.zip, s.state, s.city, s.student_type
  FROM students s
  JOIN certifications c ON c.student_id = s.student_id
  LEFT JOIN certifications c2 ON c2.student_id = s.student_id
AND c2.expiration > DATE_ADD(NOW(), INTERVAL 1 YEAR)
  JOIN cert_type ct ON ct.cert_type=c.cert_type
  WHERE ( s.student_id = c.student_id )
    AND s.status='A' 
    AND s.student_type != 'B' 
    AND s.student_type != 'D'  
    AND s.student_type != 'E'
    AND s.student_type != 'W'  
    AND s.student_type != 'T'  
    AND s.student_type != 'I'  
    AND c.expiration >= CURDATE() AND c.expiration <= DATE_ADD(NOW(), INTERVAL 1 YEAR)
    AND c2.student_id is null
    ORDER BY s.lname, s.fname
于 2012-10-29T17:49:06.050 回答