Count all rows containing [Word] in [Column] in postgresql - postgresql

I need to count and also get output of all rows in a table containing a given word in a specific column. Something like
ID Name Fave
678 Adam cannot bear this
355 Baloo bear is a bear
245 Cheeta runs fast
So that I can get an output of '2' (and not '3') on counting the number of rows containing the word 'bear' in the column 'Fave', and an output of the first two rows for the tabular output/select rows.
I've tried
SELECT * WHERE regexp_matches(Fave, 'bear') FROM table_name
but I'm getting a syntax error near FROM so I'm WHERE is where the trouble is at. Any pointers/help, please?

Are you looking for:
SELECT * FROM table_name WHERE Fave like '%bear%'

The FROM goes before the WHERE:
SELECT *
FROM table_name
WHERE regexp_matches(Fave, 'bear') ;
You can also use LIKE, of course, but the issue is the order of the clauses in the query.

select * from table_name where Fave ~* '\mbear\M';
~* - case-insensitive regexp matches
'\m...\M' - single word, so 'taddy bear' is matching and taddybear is not.

Related

Counting the Number of Occurrences of a Multi-Word Phrase in Text with PostgreSQL

I have a problem, I need to count the frequency of a word phrase appearing within a text field in a PostgreSQL database.
I'm aware of functions such as to_tsquery() and I'm using it to check if a phrase exists within the text using to_tsquery('simple', 'sample text'), however, I'm unsure of how to count these occurrences accurately.
If the words are contained just once in the string (I am supposing here that your table contains two columns, one with an id and another with a text column called my_text):
SELECT
count(id)
FROM
my_table
WHERE
my_text ~* 'the_words_i_am_looking_for'
If the occurrences are more than one per field, this nested query can be used:
SELECT
id,
count(matches) as matches
FROM (
SELECT
id,
regexp_matches(my_text, 'the_words_i_am_looking_for', 'g') as matches
FROM
my_table
) t
GROUP BY 1
The syntax of this function and much more about string pattern matching can be found here.

Exclude a word in sql query?

I'm trying to write a query in sql to exclude a keyword:
It's a list of cities written out (e.g. AnnArbor-MI). In the list there are duplicates because some have the word 'badsetup' after the city and these need to be discarded. How would I write something to exclude any city with 'badsetup' after it?
Your question title and content appear to be asking for two different things ...
Query cities while excluding the trailing 'badsetup':
SELECT regexp_matches(citycolumn, '(.*)badsetup')
FROM mytable;
Query cities that don't have the trailing 'badsetup':
SELECT citycolumn
FROM mytable
WHERE citycolumn NOT LIKE '%badsetup';
In psql, to select rows excluding those with the word 'badsetup' you can use the following:
SELECT * FROM table_name WHERE column NOT LIKE '%badsetup%';
In this case the '%' indicates that there can be any characters of any length in this space. So this query will find any instance of the phrase 'badsetup' in your column, regardless of the characters before or after it.
You can find more information in section 9.7.1 here: https://www.postgresql.org/docs/8.3/static/functions-matching.html

Alternative when IN clause is inputed A LOT of values (postgreSQL)

I'm using the IN clause to retrieve places that contains certain tags. For that I simply use
select .. FROM table WHERE tags IN (...)
For now the number of tags I provide in the IN clause is around 500) but soon (in the near future) number tags will probably jump off to easily over 5000 (maybe even more)
I would guess there is some kind of limition in both the size of the query AND in the number values in the IN clause (bonus question for curiosity what is this value?)
So my question is what is a good alternative query that would be future proof even if in the future I would be matching against let's say 10'000 tags ?
ps: I have looked around and see people mentioning "temporary table". I have never used those. How will they be used in my case? Will i need to create a temp table everytime I make a query ?
Thanks,
Francesco
One option is to join this to a values clause
with parms (tag) as (
values ('tag1'), ('tag2'), ('tag3')
)
select t.*
from the_table t
join params p on p.tag = t.tag;
You could create a table using:
tablename
id | tags
----+----------
1 | tag1
2 | tag2
3 | tag3
And then do:
select .. FROM table WHERE tags IN (SELECT * FROM tablename)

postgres, substring a subselect

I have a query that uses a subselect like this
SELECT "columnA","columnB", (SELECT column1 FROM tableB WHERE id=1 LIMIT 1) as text
FROM tableA WHERE id=1
Now i would like to only get the last 3 chars from my "as text" column. I have tried to apply the substring or right around my subselect but that returns an error, can anyone explain why and how to do this properly?
You need to use internal function substring matching POSIX regular expression
SELECT "columnA","columnB", (SELECT substring(column1::TEXT from '...$') FROM tableB WHERE id=1 LIMIT 1) as text
FROM tableA WHERE id=1
Please keep in mind that this way, if you have more than 1 record in tableA that matches your WHERE criteria, you will still be getting the same value in variable text for this query.

How do I find a word that certain rows contain in SQLite?

I am having a hard time figuring out how to do the following in SQLite:
I have a table with let's say the following:
table name: terms
golden
waterfall
inception
castaway
I would like to now do a lookup on all of the terms in the table that is contained in a specific string. So a string like "abc_golden#hotmail.com" should return a match. Or "life_waterfall_5" should return a match.
I understand how to do this with the LIKE statement if it was the other way around (if I was looking for matches in the table that contains a specific word. But how do I do it in my case where I have to match all entries that is contained WITHIN my search term?
To find rows that contain a string:
SELECT * FROM tbl WHERE col LIKE '%word%';
To find rows that a string contains, just turn it backwards:
SELECT * FROM tbl WHERE 'some string' LIKE '%' || col || '%';