user2741700
user2741700

Reputation: 901

REGEXP "and" operator

I have a database named "table" like this :

id  content
1     a
2     a b c
3     b a
4     c e a

and I want to get all the id where "a" and "c" are in content.

I know that I can find all the id where "a" or "c" are in content like this :

select id
from table
where content regexp 'a|c'

but I don't know what to use for "AND" operator

Upvotes: 0

Views: 573

Answers (2)

Travesty3
Travesty3

Reputation: 14479

Instead of using a regex for this, I would just use LIKE:

SELECT
    id
FROM
    `table`
WHERE
    content LIKE '%a%'
    AND content LIKE '%c%'

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149050

You could use a pattern like this:

a.*c|c.*a

This matches an a followed by any number of characters and a c or a c followed by any number of characters and an a.

Alternatively, you could just chain two where clauses together:

select id
from table
where content regexp 'a' AND
      content regexp 'c'

Upvotes: 0

Related Questions