題目描述
假如A公司為了調(diào)查用戶對(duì)于某產(chǎn)品的滿意情況,設(shè)置了一套調(diào)查問(wèn)卷,這套問(wèn)卷一共有五道題,每個(gè)題的分值均在于0~10,得到調(diào)研結(jié)果統(tǒng)計(jì)表questions如下,其中id代表用戶,items代表問(wèn)卷中的問(wèn)題,scores代表用戶給出的分?jǐn)?shù)。
| id | items | scores |
|---|---|---|
| 1 | 1 | 8 |
| 1 | 2 | 9 |
| 1 | 3 | 7 |
| 1 | 4 | 4 |
| 1 | 5 | 3 |
| 2 | 1 | 10 |
| 2 | 2 | 10 |
| 2 | 3 | 10 |
| 2 | 4 | 10 |
| 2 | 5 | 10 |
| 3 | 1 | 3 |
| 3 | 2 | 2 |
| 3 | 3 | 1 |
| 3 | 4 | 2 |
| 3 | 5 | 3 |
問(wèn)題
(1)使用sql代碼查出給所有問(wèn)題打分均相同的用戶,例如上述用戶2。
(2)是用sql代碼查出給所有問(wèn)題打分均低于5分的用戶,例如上述用戶3。
答案
(1)
select q.id
from (select id, count(distinct scores) as counting
from questions group by id)
as q where q.counting = 1;
(2)
select id from questions group by id having max(scores) < 5;