سؤال

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