This content originally appeared on DEV Community and was authored by Augusts Bautra
Today I encountered an interesting way to join two tables for the first time - using an operator other than id = other.id
.
I have a config_entries
table that holds the beginning parts of Thing names that need to be handled in a special way. How to query for the Things that qualify? JOIN with LIKE to the rescue!
Thing.all.joins(
"INNER JOIN config_entries ON " \
" thing.name LIKE config_entries.lookup_term || '%'"
)
The LIKE config_entries.lookup_term || '%'
portion is PSQL concatenation, equivalent to LIKE CONCAT(config_entries.lookup_term, '%')
, resulting in a term%
argument for LIKE.
This content originally appeared on DEV Community and was authored by Augusts Bautra
Augusts Bautra | Sciencx (2024-11-01T15:09:25+00:00) TIL: joining with LIKE. Retrieved from https://www.scien.cx/2024/11/01/til-joining-with-like/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.