I have two columns say Main and Sub. (they can be of same table or not).
Main is varchar of length 20 and Sub is varchar of length 8.
Sub is always subset of Main and it is last 8 characters of Main.
I could successfully design a query to match pattern using substr("Main",13,8)
Query:
select * from "MainTable"
where substr("MainColumn",13,8) LIKE (
select "SubColumn" From "SubTable" Where "SubId"=1043);
but I want to use Like, % , _ etc in my query so that I can loosely match the pattern (that is not all 8 characters).
Question is how can i do that.?!
I know that the query below is COMPLETELY WRONG but I want to achieve something like this,
Select * from "MainTable"
Where "MainColumn" Like '%' Select "SubColumn" From "SubTable" Where "SubId"=2'
The answers so far fail to address your question:
It makes hardly any difference whether you use
LIKEor=as long as you match the whole string (and there are no wildcard character in your string). To make the search fuzzy, you need to replace part of the pattern, not just add to it.For instance, to match on the last 7 (instead of 8) characters of
subcolumn:I use the simpler
left()(introduced with Postgres 9.1).You
couldsimplify this to:But you wouldn’t if you use the special index I mention further down, because expressions in functional indexes have to matched precisely to be of use.
You may be interested in the extension
pg_tgrm.In PostgreSQL 9.1 run once per database:
Two reasons:
It supplies the similarity operator
%. With it you can build a smart similarity search:It supplies index support for both
LIKEand%If read performance is more important than write performance, I suggest you create a functional GIN or GiST index like this:
This index supports either query. Be aware that it comes with some cost for write operations.
A quick benchmark for a similar case in this related answer.