MySQL SELECT values with more than three words

元气小坏坏 提交于 2020-01-25 11:39:25

问题


I currently have the following code

select *
FROM list
WHERE name LIKE '___'
ORDER BY name;

I am trying to get it so that it only shows names with three or more words. It only displays names with three characters I cannot seem to work out the correct syntax for this.

Any help is appreciated Thankyou


回答1:


If you assume that there are no double spaces, you can do:

WHERE name like '% % %'

To get names with three or more words.

If you can have double spaces (or other punctuation), then you are likely to want a regular expression. Something like:

WHERE name REGEXP '^[^ ]+[ ]+[^ ]+.*$'



回答2:


I do believe what you are after is

SELECT *
FROM list
WHERE name LIKE '% % %'
ORDER BY name;



回答3:


you can count number of words and then select those who are equal or greater then 3 words.

    SELECT * FROM list
    HAVING LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 >= 3
    ORDER BY name

DEMO HERE

*Even if you have multi spaces it will not affect here check this



来源:https://stackoverflow.com/questions/25766281/mysql-select-values-with-more-than-three-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!