I have a question which is basically the same as this , except that I want to implement it using the Django ORM if possible.
The linked question says:
How would you search for the longest match within a varchar variable? For example, the_table has entries as follows:
magic_word | prize
===================
sh| $0.20
sha| $0.40
shaz| $0.60
shaza| $1.50
I would like to write a plpgsql function that takes amongst other arguments a string as input (e.g. shazam
), and returns the 'prize' column on the row of the_table with the longest matching substring. In the example shown, that would be $1.50
on the row with magic_word shaza
.
The answer I want to emulate in Django is
SELECT magic_word
FROM the_table
WHERE 'shazam' LIKE (magic_word || '%')
ORDER BY magic_word DESC
LIMIT 1;
... but I can't see how to do the "backwards" LIKE
statement.
("Backwards" in the sense that it's the input variable that on the lhs of the LIKE
)