[英]Returning all records and displays only 1 record only when there is more than 1 of the same record
[英]MYSQL - Get all records that have more than 1 record for the same id
如果我沒有正確解釋這個問題,我會提前道歉。 我幾乎無法用英語來解釋它,更不用說在mysql查詢中。
我想獲得的名單response_set_ids
有一個超過1個記錄question_id
。
這是我的數據示例:
+----+-----------------+-------------+-----------+
| id | response_set_id | question_id | answer_id |
+----+-----------------+-------------+-----------+
| 1 | 10 | 1 | 4 |
| 2 | 10 | 2 | 5 |
| 3 | 10 | 3 | 6 |
| 4 | 10 | 3 | 7 |
| 5 | 11 | 1 | 8 |
| 6 | 11 | 2 | 9 |
| 7 | 11 | 3 | 10 |
+----+-----------------+-------------+-----------+
我想有一個查詢,它會返回一個列表response_set_ids,在這個特定的例子中,我希望得到返回10
因為response_set有question_id -> 3
出現不止一次。
如果您需要任何進一步的信息來幫助我,請告訴我。
我試過這個: select response_set_id, count(question_id) from responses group by response_set_id;
但這只能給出每個響應集的問題數。
先謝謝你!
select distinct response_set_id from (
select response_set_id , question_id
from
responses
group by
response_set_id, question_id
having count(*)>1) a
最簡單的方法不使用子查詢:
SELECT DISTINCT response_set_id
FROM responses
GROUP BY response_set_id, question_id
HAVING COUNT(*) > 1;
這是非常極少數情況之一,其中select distinct
)與group by
一起使用(適當)。
我相信此問題之前已被提出,但我不能在我當前的代表處添加評論:
SELECT DISTINCT response_set_id
FROM responses
GROUP BY question_id
HAVING COUNT(question_id) > 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.