0

Command should do: Give 1 as output if the pattern "*@he.com" is on the row excluding the headings:

 user_id | username |   email   |           passhash_md5           | logged_in | has_been_sent_a_moderator_message | was_last_checked_by_moderator_at_time | a_moderator 
---------+----------+-----------+----------------------------------+-----------+-----------------------------------+---------------------------------------+-------------
       9 | he       | [email protected] | 6f96cfdfe5ccc627cadf24b41725caa4 |         0 |                                 1 | 2009-08-23 19:16:46.316272            | 

In short, I want to connect many SELECT-commands with Regex, rather like Unix pipes. The output above is from a SELECT-command. A new SELECT-command with matching the pattern should give me 1.

Related

1
  • 1
    Perhaps you can explain what you want to achieve? From the snippet you showed - I'm not sure what is it that you want to get. Commented Aug 24, 2009 at 9:44

3 Answers 3

2

Did you mean

SELECT regexp_matches( (SELECT whatevername FROM users WHERE username='masi'), 'masi');

you obviously can not feed the record (*) to regexp_matches, but I assume this is not what your problem is, since you mention the issue of nesting SQL queries in the subject.

Maybe you meant something like

SELECT regexp_matches( wn, 'masi' ) FROM (SELECT whatevername AS wn FROM users WHERE username LIKE '%masi%') AS sq;

for the case when your subquery yields multiple results.

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

5 Comments

You are probably rigth. "ERROR: subquery must return only one column" with the first command.
I suspect you've left your * there, because subquery SELECT whatevername may not return more than one column. Or what did you mean by "first command"?
Are you sure? I tested it with the command "SELECT regexp_matches( (SELECT * FROM users WHERE username='he'), '*he.com');" to the table added to the question.
Read my query carefully. Not only I haven't written SELECT * there, but I also explicitly commented, saying that you obviously can not feed the record () to regexp_matches*. You need to put the field name there, not asterisk (unless you only have one field).
Alias given to subselect. Subselects acts like a table in the FROM list, but obviously needs a name to be referred to (it is required for subqueries), no matter if you need to refer to it by name.
1

It looks like you could use a regular expression query to match on the email address:

select * from table where email ~ '.*@he.com';

To return 1 from this query if there is a match:

select distinct 1 from table where email ~ '.*@he.com';

This will return a single row containing a column with 1 if there is a match, otherwise no rows at all. There are many other possible ways to construct such a query.

4 Comments

?column? ---------- 1 (1 row)
Why do I get the meta-data besides 1? Is it possible to filter it in Postgres?
you mean the column header? As far as I can tell you're using psql shell, perhaps what you're asking for is psql -t option (tuples only).
Right, the decoration around the data is just psql giving you a nice presentation. When you execute the query from a program, your program won't see the column headers or dividing lines.
1

Let's say that your original query is:

select * from users where is_active = true;

And that you really want to match in any column (which is bad idea for a lot of reasons), and you want just to check if "*@he.com" matches any row (by the way - this is not correct regexp! correct would be .*@he.com, but since there are no anchors (^ or $) you can just write @he.com.

select 1 from (
    select * from users where is_active = true
) as x
where textin(record_out( x )) ~ '@he.com'
limit 1;

of course you can also select all columns:

select * from (
    select * from users where is_active = true
) as x
where textin(record_out( x )) ~ '@he.com'
limit 1;

Comments

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.