mysql 中的 max(length(field))
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2357620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
max(length(field)) in mysql
提问by JPro
If I say:
如果我说:
select max(length(Name))
from my_table
I get the result as 18, but I want the concerned data also. So if I say:
我得到的结果是 18,但我也想要相关的数据。所以如果我说:
select max(length(Name)),
Name
from my_table
...it does not work. There should be a self join I guess which I am unable to figure it out.
...这是行不通的。应该有一个自我加入,我想我无法弄清楚。
Can anyone please provide me a clue?
任何人都可以给我一个线索吗?
回答by Quassnoi
SELECT name, LENGTH(name) AS mlen
FROM mytable
ORDER BY
mlen DESC
LIMIT 1
回答by cjohn
Edited, will work for unknown max() values:
已编辑,适用于未知的 max() 值:
select name, length( name )
from my_table
where length( name ) = ( select max( length( name ) ) from my_table );
回答by Velizar Andreev Kitanov
回答by Merish Joseph
Select URColumnName From URTableName Where length(URColumnName ) IN
(Select max(length(URColumnName)) From URTableName);
This will give you the records in that particular column which has the maximum length.
这将为您提供具有最大长度的特定列中的记录。
回答by Suman
In case you need both max and min from same table:
如果您需要同一个表中的最大值和最小值:
select * from (
(select city, length(city) as maxlen from station
order by maxlen desc limit 1)
union
(select city, length(city) as minlen from station
order by minlen,city limit 1))a;
回答by OMG Ponies
Use:
用:
SELECT mt.name
FROM MY_TABLE mt
GROUP BY mt.name
HAVING MAX(LENGTH(mt.name)) = 18
...assuming you know the length beforehand. If you don't, use:
...假设您事先知道长度。如果没有,请使用:
SELECT mt.name
FROM MY_TABLE mt
JOIN (SELECT MAX(LENGTH(x.name) AS max_length
FROM MY_TABLE x) y ON y.max_length = LENGTH(mt.name)
回答by Martin
select *
from my_table
where length( Name ) = (
select max( length( Name ) )
from my_table
limit 1
);
It this involves two table scans, and so might not be very fast !
这涉及两次表扫描,因此可能不会很快!
回答by Rajesh Goel
Use CHAR_LENGTH() instead-of LENGTH() as suggested in: MySQL - length() vs char_length()
按照以下建议使用 CHAR_LENGTH() 而不是 LENGTH(): MySQL - length() vs char_length()
SELECT name, CHAR_LENGTH(name) AS mlen FROM mytable ORDER BY mlen DESC LIMIT 1
SELECT name, CHAR_LENGTH(name) AS mlen FROM mytable ORDER BY mlen DESC LIMIT 1
回答by Pascal MARTIN
I suppose you could use a solution such as this one :
我想你可以使用这样的解决方案:
select name, length(name)
from users
where id = (
select id
from users
order by length(name) desc
limit 1
);
Might not be the optimal solution, though... But seems to work.
不过,可能不是最佳解决方案......但似乎有效。