After calculating the number of posts for each group using "count(name)," we want to output the results where the count is greater than 5 (count(name)>5). This requires using "having" to set the condition for filtering the results after grouping. The difficulty here lies in understanding the use of "having."
正確答案是:B: select id, count(name) from t1 group by id having count(name)>5;
專業(yè)分析:
1. **選項(xiàng)A**: `select id, count(name) from t1 group by id;`
- 這個(gè)語(yǔ)句會(huì)按照`id`分組,并計(jì)算每個(gè)`id`對(duì)應(yīng)的`name`的數(shù)量。但是,它沒(méi)有篩選出帖子數(shù)量大于5的條件。
2. **選項(xiàng)B**: `select id, count(name) from t1 group by id having count(name)>5;`
- 這個(gè)語(yǔ)句不僅按照`id`分組并計(jì)算每個(gè)`id`對(duì)應(yīng)的`name`的數(shù)量,還通過(guò)`having`子句篩選出帖子數(shù)量大于5的記錄。因此,這是正確的答案。
3. **選項(xiàng)C**: `select id, count(name) from t1 group by id having count(name)>5 order by count(name);`
- 這個(gè)語(yǔ)句與選項(xiàng)B類似,但多了一個(gè)`order by`子句,用于按帖子數(shù)量排序。雖然語(yǔ)法正確,但題目只要求篩選出帖子數(shù)量大于5的記錄,并沒(méi)有要求排序,因此不是最佳答案。
4. **選項(xiàng)D**: `select id, count(name) from t1 where id > 100 group by id;`
- 這個(gè)語(yǔ)句在分組之前增加了一個(gè)`where`條件,篩選出`id`大于100的記錄。這個(gè)條件與題目要求無(wú)關(guān),因此不是正確答案。
綜上所述,選項(xiàng)B最符合題目要求。