Let's say, I have 2 product names: dead space and dead space limited edition, and I want to group them by "product_name" to select "dead space" in a mysql query. So I want the query to select the "product_name" with the shortest string if the where caluse is "product_name LIKE '%dead space%'".

Thanks a lot,

有帮助吗?

解决方案

you may try LENGTH().

 select product_name ,length(product_name) as the_length from your_table 
 where product_name LIKE '%dead space%'
 ORDER BY  length(product_name)
 limit 1

DEMO HERE

其他提示

Please try this query

 select * from table where product_name LIKE '%dead space%'
 HAVING length(product_name ) = min(length(product_name ))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top