jerry
Ecto query using like/ilike in query
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
product="SurfacePro"
from(u in Product,
where: like(u.product_name, %product%),
select: %{product_id: u.id, description: u.product_desc}
) |> Repo.all
Most Liked
NobbZ
Have you taken a look at the documentation? https://hexdocs.pm/ecto/Ecto.Query.API.html#like/2
From how I read it, you need to pass in a string as second argument, eg. like(u.product_name, "%SurfacePro%"), but you wan’t this probably set dynamically at runtime from user input.
Since I’m not sure if you are allowed to interpolate a string in the query the way you would do it outside, I’ll prepare it and then pin:
product = "SurfacePro" # or from a function argument?
like = "%#{product}%"
from(u in Product,
where: like(u.product_name, ^like),
select: %{product_id: u.id, description: u.product_desc}
) |> Repo.all
jerry
rameshsharma
Hi Luca,
Thanks for responding. I posted here as I thought it could be linked to like search. S.orry for that
The roles column in database is a Postgres array of strings. You are correct.
I want to get the count of rows that include ‘super’ among the roles?







