Question

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,

Was it helpful?

Solution

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

OTHER TIPS

Please try this query

 select * from table where product_name LIKE '%dead space%'
 HAVING length(product_name ) = min(length(product_name ))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top