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? Ecto.Query.API — Ecto v3.14.0
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?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










