mysql中的聚簇表查询

发布于 2025-01-06 21:52:54 字数 270 浏览 4 评论 0原文

我的 mysql 数据库中有一个表,其中有一列 score 和一列 cluster

score 是浮点数,cluster 可以是 NULL 或整数。

NULL 表示该行不属于任何簇,整数表示属于该簇。所以当然多行可以有相同的簇ID。

我想选择所有为 NULL 的行或集群中得分最高的行。

是否可以在选择或选择/联合星座或临时表中执行?最快的方法是什么?

I have a table in my mysql database which has a column score and a column cluster.

score is float and cluster may be NULL or integer.

NULL means the row belongs to no cluster, an integer means it does. so of course multiple rows can have the same cluster id.

I want to select all rows that are either NULL or the ones of a cluster that have the highest score.

Is it possible to do in a select or in a select/union constellation or with temperary tables? what is the fastest way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-01-13 21:52:54

如果我理解你的问题,那么:

SELECT data.* FROM data WHERE data.cluster IS NULL

UNION

SELECT data.*
FROM       (SELECT cluster, MAX(score) AS score FROM data GROUP BY cluster)
        AS max_score
INNER JOIN data  ON data.cluster = max_score.cluster
                AND data.score   = max_score.score

If I understood your question, then:

SELECT data.* FROM data WHERE data.cluster IS NULL

UNION

SELECT data.*
FROM       (SELECT cluster, MAX(score) AS score FROM data GROUP BY cluster)
        AS max_score
INNER JOIN data  ON data.cluster = max_score.cluster
                AND data.score   = max_score.score
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文