如何在分组时根据一个字段中最长的“字符串”从mysql数据库中选择记录?

时间:2017-02-01 05:41:39

标签: java mysql sql string select

这是我的表:

ID    Title    Content    Cluster
1       a       abc         1
2       b       abcde       1
3       c       abcde       1
4       d       abc         2
5       e       abcdef      2

我想根据字段内容中最长的“字符串”和逐个群组选择记录。我怎么能这样做。

期望的结果:

ID    Title    Content    Cluster
3       c       abcde       1
5       e       abcdef      2

怎么做?

2 个答案:

答案 0 :(得分:0)

使用自联接,您可以找出每个群集中具有最大长度的所有行:

select a.*
from my_table a
left join my_table b
on a.cluster = b.cluster
and length(a.content) < length(b.content)
where b.cluster is null;

SQL Fiddle Demo

另一种方法是在子查询中使用group by:

select a.*
from my_table a
inner join (
    select cluster, max(length(content)) len
    from my_table
    group by cluster
) b on a.cluster = b.cluster and length(a.content) = b.len;

此外,如果内容包含Unicode字符,则使用char_length代替length函数。

SQL Fiddle Demo

编辑:

为了清除长度上的关系,您可以获得id最高的行(或根据您的需要最低):

select a.*
from cluster_sosial a
left join cluster_sosial b
on a.cluster = b.cluster
and (
    length(a.content) < length(b.content)
    or (length(a.content) = length(b.content) and a.cluster_sosial_id < b.cluster_sosial_id)
)
where b.cluster is null;

Demo

使用相关子查询

SELECT 
    *
FROM
    cluster_sosial a
WHERE
    cluster_sosial_id = (SELECT 
            cluster_sosial_id
        FROM
            cluster_sosial B
        WHERE
            a.Cluster = b.Cluster
        ORDER BY LENGTH(content) DESC , cluster_sosial_id DESC
        LIMIT 1)

Demo

答案 1 :(得分:0)

以下是使用EXISTS

的一种方法
SELECT *
FROM   Yourtable a
WHERE  EXISTS (SELECT 1
               FROM   Yourtable B
               WHERE  a.Cluster = b.Cluster
               HAVING Max(Len(b.Content)) = Len(a.Content)) 

SELECT *
FROM   yourtable a
WHERE  Content = (SELECT content
                    FROM   yourtable  B
                    WHERE  a.Cluster = b.Cluster
                    ORDER  BY Len(content) DESC Limit 1)