3

i have a table like this:

book_no     lang      price      shelf     
----------  --------  ---------  -----
1           eng       20         a  
4           french    34         a  
2           eng       26         b  
7           russian   71         b  
12          german    33         a  
43          french    15         d  
11          eng       43         c  
14          rusian    33         a  
19          eng       20         d  
24          french    40         c  
23          eng       57         a  
27          russian   48         b  
42          german    31         c  
25          french    15         d  

from this table i want to fetch book_no, language, shelf.
The languages which are present more than 3 times, only those books are to be shown.
I tried:

select book_no,lang,shelf from a where (count(lang)>3)    

thanks for help in advance

4

3 回答 3

4
SELECT book_no, 
       lang, 
       shelf 
FROM   a 
WHERE  lang IN (SELECT lang 
                FROM   A 
                GROUP  BY lang 
                HAVING Count(*) > 3) 
于 2012-05-24T11:05:22.393 回答
1

不能在 where 子句中使用 count、sum 等分组函数。

使用群组

group by book_no -- or the fields you need, depending on sgbd, you can put one or have to put all

和一个有子句

Having count(lang) > 3
于 2012-05-24T11:01:34.850 回答
0

如果你的 DBMS 支持聚合窗口函数,你可以试试这个:

SELECT
  book_no,
  lang,
  shelf
FROM (
  SELECT
    *,
    COUNT(*) OVER (PARTITION BY lang) AS book_count
  FROM a
) s
WHERE book_count >= 3
于 2012-05-24T12:53:53.857 回答