Question

I want to retrieve all rows that have that have a certain column with only 2 words. I tried this, but it failed:

SELECT DISTINCT sentence FROM vocab WHERE p='adj' AND sentence REGEXP '^[a-zA-Z]+\s[a-zA-Z]+$'

This doesn't seem to work. Words can have ! or ? in it, so I don't think [a-zA-Z] is appropriate either way.

EDIT: I should rephrase it a bit, it should match any "group" of characters. Meaning

xczc zczxc~

1233 iojsd

´+a s~º

Could I use something like (.*) ?

Was it helpful?

Solution

MySQL doesn't support \s so you can use [[:space:]] instead, you can try this:

SELECT DISTINCT sentence 
FROM vocab 
WHERE p='adj' 
AND sentence RLIKE '^[[:punct:][:alnum:]]+[[:space:]][[:punct:][:alnum:]]+$'

It'll look for rows where sentence is 2 words which may contain letters, ? or !

Sample SQLFiddle

OTHER TIPS

Not very pretty but this should work

'^[a-zA-Z?!]+[[:space:]][a-zA-Z?!]+$'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top