簡體   English   中英

MySQL:在一組不同的(char_length(text_column))記錄中選擇頂部結果

[英]MySQL: select top result in a group of distinct(char_length(text_column)) records

我有一組看起來像以下名義結果的記錄。 有多次重復計數列中的數字。 “計數”來自char_length(text_column)。 我想獲取每個此類分組中的第一條記錄。 我該怎么做呢?

我查看了char_length()文檔,並搜索了已完成此操作的示例,但找不到任何內容。

select distinct(char_length(text_column)), text_column from table

....與我已經得出的結果一樣接近-顯然不是很接近。

=======+============================
 count +      text_column
=======+============================
2139   + some text entry
-------+----------------------------
3000   + another sentence here
-------+----------------------------
3000   + more text in this sentence
-------+----------------------------
3000   + still more text here
-------+----------------------------

謝謝BW

除非有列指定順序,否則沒有“ first”之類的東西。 但是,您可以使用group by不確定的行中獲取值:

select char_length(text_column), text_column
from table
group by char_length(text_column);

我不使用MySQL擴展到一個大風扇group by在那里text_column被允許在select ,而不是group by 但是,因為它是一長列,所以僅讓MySQL選擇值而不是使用實際的聚合函數(例如min()max() )可能會更有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM