I
I
Ivan Nesterovich2015-07-24 00:56:14
PostgreSQL
Ivan Nesterovich, 2015-07-24 00:56:14

How to perform sequential fetch in PostgreSQL?

I need to make a request to the database and get an entry with path='/a/b/c/d' if there is no such entry, then path='/a/b/c' and so on. That is, at the output I have one record that was found in that order.
How to implement it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2015-07-24
@BuriK666

prepare a list of possible paths

SELECT * FROM table 
WHERE path IN ('/a/b/c/d', '/a/b/c', ...) ORDER BY char_length(path) DESC LIMIT 1;

Well, do not forget to do an index by char_length (path) ..
CREATE INDEX ON TABLE (path);
CREATE INDEX ON TABLE (char_length(path));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question