mysql group by隐藏,mysql-group by子句列名而不在选择列表中选择

select person

from person

inner join collectionmember

on sourceobjectid=personid

group by sourceobjectid

having count(sourceobjectid)>1;

据说如果使用group by子句,则select列列表中必须存在group by列名,但是上面的查询没有选择sourceobjectid.

解决方法:

结果不是遇到的第一行. MySQL非常清楚所谓“隐藏列”的使用.引用documentation:

MySQL extends the use of GROUP BY so that the select list can refer to

nonaggregated columns not named in the GROUP BY clause. This means

that the preceding query is legal in MySQL. You can use this feature

to get better performance by avoiding unnecessary column sorting and

grouping. However, this is useful primarily when all values in each

nonaggregated column not named in the GROUP BY are the same for each

group. The server is free to choose any value from each group, so

unless they are the same, the values chosen are indeterminate.

Furthermore, the selection of values from each group cannot be

influenced by adding an ORDER BY clause. Sorting of the result set

occurs after values have been chosen, and ORDER BY does not affect

which values within each group the server chooses.

此扩展的目的是允许查询在表的主键上进行分组的位置,而不必放在所有其他列中.此行为实际上与ANSI标准一致.

在其他情况下,可能会使用“隐藏列”.选择的值是任意的. MySQL甚至不保证它们来自同一行(尽管实际上是这样).

标签:sql,mysql

来源: https://codeday.me/bug/20191122/2062987.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值