MySQL用户定义函数用于获取列的模态值?
例如:
SELECT MODE(field) FROM table
在另一种模式下,我可以使用什么用户定义函数来获取列的最常见值?
我知道我可以这样做:
SELECT field, COUNT(*) as total FROM table GROUP BY field ORDER BY total DESC LIMIT 1
但我也必须在同一个 MySQL 语句中查询其他数据,所以我必须使用用户定义的函数。
谢谢。
For example:
SELECT MODE(field) FROM table
In another mode, what user-defined function can I use to get the most common value of a column?
I know I can do something like:
SELECT field, COUNT(*) as total FROM table GROUP BY field ORDER BY total DESC LIMIT 1
But I have to query other data in the same MySQL statement too, so I have to use a user-defined function.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是MySQL 关于聚合函数的文档的链接。看起来他们没有任何“模式”,所以我想说你的第二个查询可能是你最好的选择。
Here's a link to MySQL's documentation on aggregate functions. It looks like they don't have anything for "mode", so I would say that your second query is probably your best shot.
MySQL 不支持用户定义的聚合函数(PostgreSQL 支持,例如它的价值是什么)。您不能使用 UDF 在 MySQL 中执行您想要的操作。
例如,您可以通过将模式计算放入派生表子查询中来实现:
MySQL doesn't support user-defined aggregate functions (PostgreSQL does, for what it's worth). You can't use a UDF to do what you want in MySQL.
You can do it for example by putting the mode-computation in a derived table subquery: