0

I'm trying to match words from a table to a string when only the exact word matches, I've only been able to get very loose match so far by using a LIKE statement and it matches most things but the issue is it also matches partial words which I'm trying to avoid.

There isn't always a leading or trailing space.

SQL Query

SELECT GROUP_CONCAT(`keys` SEPARATOR ',')
FROM `table_keys`
WHERE "We Have One Cardboard Train Wheel" LIKE CONCAT('%', `keys`, '%');

table_keys

| keys      |
|-----------|
| Car       |
| Wheel     |
| Roof      |

Two matchs are returned which is Car, Wheel which is technically correct but I only want it to match if the whole word is present.

Current Output

Car,Wheel

Wanted Output

Wheel

1 Answer 1

0

Regular expressions are Strings

CREATE TABLE table_keys (

  `keys` VARCHAR(5)

);

INSERT INTO table_keys
  ( `keys`)
VALUES
  ('Car'),
  ( 'Wheel'),
  ( 'Roof');
SELECT GROUP_CONCAT(`keys` SEPARATOR ',') AS "keys"
FROM `table_keys`
WHERE "We Have One Cardboard Train Wheel"  
regexp CONCAT('(^|[[:space:]])',`keys`,'([[:space:]]|$)')
| keys  |
| :---- |
| Wheel |

db<>fiddle here

Sign up to request clarification or add additional context in comments.

2 Comments

it's a step in the right direction be I can't guarantee there will always be a leading or trailing space.
This seems to work, REGEXP CONCAT('[[:<:]]' , keys , '[[:>:]]').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.