TIL: joining with LIKE

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 que…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » TIL: joining with LIKE." Augusts Bautra | Sciencx - Friday November 1, 2024, https://www.scien.cx/2024/11/01/til-joining-with-like/
HARVARD
Augusts Bautra | Sciencx Friday November 1, 2024 » TIL: joining with LIKE., viewed ,<https://www.scien.cx/2024/11/01/til-joining-with-like/>
VANCOUVER
Augusts Bautra | Sciencx - » TIL: joining with LIKE. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/01/til-joining-with-like/
CHICAGO
" » TIL: joining with LIKE." Augusts Bautra | Sciencx - Accessed . https://www.scien.cx/2024/11/01/til-joining-with-like/
IEEE
" » TIL: joining with LIKE." Augusts Bautra | Sciencx [Online]. Available: https://www.scien.cx/2024/11/01/til-joining-with-like/. [Accessed: ]
rf:citation
» TIL: joining with LIKE | Augusts Bautra | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.