vote up 0 vote down star
1

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

flag

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. – depesz Aug 24 at 9:44

3 Answers

vote up 1 vote down check

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.

link|flag
You are probably rigth. "ERROR: subquery must return only one column" with the first command. – Masi Aug 24 at 9:13
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"? – Michael Krelin - hacker Aug 24 at 10:12
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. – Masi Aug 24 at 12:23
1  
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). – Michael Krelin - hacker Aug 24 at 13:01
1  
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. – Michael Krelin - hacker Aug 24 at 19:01
show 1 more comment
vote up 1 vote down

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.

link|flag
?column? ---------- 1 (1 row) – Masi Aug 24 at 12:25
Why do I get the meta-data besides 1? Is it possible to filter it in Postgres? – Masi Aug 24 at 12:26
1  
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). – Michael Krelin - hacker Aug 24 at 12:58
1  
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. – Greg Hewgill Aug 24 at 19:42
vote up 1 vote down

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;
link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.