2014-04-18 592 views
1

我知道別名使用別名不工作,但無法弄清楚得到得到預期的結果「和count_of_circs = circ_limit」SQL在WHERE子句

select libraryid, 
      patronid, 
      count(circ.id) as count_of_circs, 
      (
       select account.circsperpatronmonth 
       from account 
       where circ.libraryid = account.libraryid 
      ) as circ_limit 
    from circ 
    where borrowed >= '2014-04-01' 
    and borrowed < '2014-04-19' 
    and count_of_circs = circ_limit 

    group by patronid,libraryid 
+1

這是什麼數據庫? – OldProgrammer

+0

你能解釋你的桌子嗎? – HaMeD

回答

2

你想要一個having條款:

select libraryid, patronid, 
     count(circ.id) as count_of_circs, 
     (select account.circsperpatronmonth 
     from account 
     where circ.libraryid=account.libraryid 
     ) as circ_limit 
From circ 
where borrowed >= '2014-04-01' and borrowed < '2014-04-19' 
group by patronid, libraryid 
having count_of_circs = circ_limit; 
0

您將需要「按組」

HAVING count_of_circs = circ_limit 

後添加一行它不是一個別名,它是過濾接振鈴後分組。