SQL distinct兩個字段,sql中的distinct

 2023-12-06 阅读 24 评论 0

摘要:測試書中題目:加上distinct是否多余?不加distinct的話,顯示類似如下情況:SELECT count( *? )FROM `players`GROUP BY towncount( * )122加上distinct的話,顯示類似如下情況:SELECT distinct count( *? )FROM `players&

測試書中題目:加上distinct是否多余?
不加distinct的話,顯示類似如下情況:
SELECT count( *? )
FROM `players`
GROUP BY town

count( * )
1
2
2

加上distinct的話,顯示類似如下情況:

SELECT distinct count( *? )
FROM `players`
GROUP BY town

count( * )
1
2

結論:加上distinct的話,重復的值不計入count中了。



另外一個測試例子:
SELECT DISTINCT playerno
FROM players
WHERE TOWN = '長沙'

distinct為什么不算重復呢?

記住機制:先按照where進行取出數據。然后按照distinct指定的列,去掉playerno值重復的列。

通俗的辦法:假如明顯看出取出的要么是一行,要么是沒有結果行。那么使用distinct是多余的。

轉載于:https://www.cnblogs.com/discuz-chen/p/4332008.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/192555.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息