0

I would like to select rows in my table by my another table with my predefined regular expressions.
Below you can see my example tables:

Table code

id   |  codename
-----+------------
1    |   123456
2    |   NF123
3    |   AAA444
4    |   EEE123
5    |   EEE423
6    |   AB123E

Table validcode

 id   |  validreg
------+-----------
 1    |   [E]{3}[0-9]{3}
 2    |   NF123

And here is the test case on sqlfidlle.

This is one of the my select statements I tried, but it doesn't work:

SELECT n.codename FROM code n
WHERE EXISTS (SELECT * FROM validcode WHERE n.codename ~* E'^' || validreg || '$');

I get this error:

ERROR: argument of WHERE must be type boolean, not type text:
SELECT codename FROM code n WHERE EXISTS (SELECT * FROM validcode WHERE n.codename ~* E'^' || validreg || '$');

Is possible to do this somehow?

2
  • You don't need where exists or subquery. You should be fine just by joining the queries, something like SELECT n.codename FROM code n, validcode WHERE n.codename ~* E'^' || validcode.validreg || '$'. Probably not very efficient, though. Commented May 17, 2014 at 20:59
  • But still it doesn't work, i think the problem is in the string where i do the concatenating somewhere here ~* E'^' || validcode.validreg || '$' Commented May 17, 2014 at 21:03

1 Answer 1

2
SELECT n.codename FROM code n, validcode
WHERE  n.codename ~* ('^' || validcode.validreg || '$')

Braces because otherwise the parser thinks that n.codename ~* E'^' is one part and to that concatenates the rest.

Also your original query would work with the additional braces.

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

1 Comment

The problem is operator precedence which is fixed properly in this answer. The original WHERE EXISTS (...) would be just as efficient as an implicit or explicit join.

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.