GROUP BY在SQL中选择哪条记录 [英] Which record will GROUP BY choose in SQL
问题描述
如果我有这样一个SQL查询:
SELECT playerno,town
FROM players
GROUP BY城镇
在这种情况下,服务器会返回让我们说一个城镇,而对于playerno,它会返回一个值,即使数据库中有多个值。
我的问题是,将选择哪个值?选择是随机的,还是会取得第一个结果,还是会发生其他事情?
谢谢。
如果您不告诉引擎要做什么,它会自行决定。它取决于引擎一些将采取第一个,其他人将采取最后一个,并有一些错误。您可以使用以下功能之一来控制此功能:
- first(fieldname)
- last (字段名称)
- 分钟(字段名称)
- max(字段名称)
- first(fieldname)
- last(fieldname)
- min(fieldname)
- max(fieldname
b
或者甚至采取一种估价或总和:通过使用su
If I have an SQL query like this one:
SELECT playerno,town
FROM players
GROUP BY town
In this case, the server will return lets say one town, and for playerno, it will return one value, even if there were multiple values in the database.
My question is, which value will be chosen? Is the choice made randomly, or will it take the first result, or something else will happen? Thank you.
If you don't tell the engine what to do , it decides for itself. Its depending on the engine some will take the first one, others will take the last one, and some give an error. You can control this by using one of those functions:
or even take an avarge or sum: by using su
这篇关于GROUP BY在SQL中选择哪条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!