获取错误 - 每个派生表都必须有自己的别名错误

时间:2012-07-16 01:30:54

标签: mysql sql mysql-error-1248

查询是获取每个表的日期,我已经检查了我的代码,但它仍然是错误的“每个派生表必须有自己的别名错误”


SELECT T.* 
    FROM (
     SELECT DISTINCT(DATE_FORMAT(p.deliverDate, '%M')) as PDate
     FROM piglet_po p 
     WHERE p.raiserID = '0025' AND p.rotationNo = '3' 

        AND p.deliverDate NOT IN 
        ( SELECT DISTINCT(DATE_FORMAT(f.issuedDate, '%M')) as FDate
           FROM feed_slip f             
           WHERE f.raiserID = '0025' AND f.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'                

           AND f.issuedDate NOT IN 
           ( SELECT DISTINCT(DATE_FORMAT(v.issuedDate, '%M')) as VDate
              FROM vet_slip
              WHERE v.raiserID = '0025' AND v.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'

            AND v.issuedDate NOT IN 
             ( SELECT DISTINCT(DATE_FORMAT(w.issuedDate, '%M')) as WDate
                FROM weight_slip w  
                WHERE w.raiserID = '0025' AND w.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'                                     
              ) 
          ) 

      ) T

1 个答案:

答案 0 :(得分:1)

您忘记了别名v

        SELECT DISTINCT(DATE_FORMAT(v.issuedDate, '%M')) as VDate
          FROM vet_slip
         WHERE v.raiserID = '0025' AND 
               v.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'

还有一个密切的括号,AFAICT。如果您只是删除了最后一个,则别名T位于错误的位置。我认为它是被丢弃的“内部括号”之一。

因此,更正的查询是:

SELECT T.* 
  FROM (SELECT DISTINCT(DATE_FORMAT(p.deliverDate, '%M')) as PDate
          FROM piglet_po p 
         WHERE p.raiserID = '0025' AND p.rotationNo = '3'
           AND p.deliverDate NOT IN 
               (SELECT DISTINCT(DATE_FORMAT(f.issuedDate, '%M')) as FDate
                  FROM feed_slip f             
                 WHERE f.raiserID = '0025'
                   AND f.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'
                   AND f.issuedDate NOT IN 
                       (SELECT DISTINCT(DATE_FORMAT(v.issuedDate, '%M')) as VDate
                          FROM vet_slip v
                         WHERE v.raiserID = '0025'
                           AND v.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'
                           AND v.issuedDate NOT IN
                               (SELECT DISTINCT(DATE_FORMAT(w.issuedDate, '%M')) as WDate
                                  FROM weight_slip w
                                 WHERE w.raiserID = '0025'
                                   AND w.issuedDate BETWEEN '2012-06-01' AND '2012-11-01'
                               ) 
                       ) 
               )
       ) T