Refining Our Results with where

Now that we know some of the query basics, we’ll start filtering the results with the where option. We’ll also see how to integrate dynamic input into queries (user input, for example), and how Ecto protects you from SQL injection attacks.

To start, we’re going to look up the id and name for the artist named “Bill Evans”:

  q = from ​"​​artists"​, ​where:​ [​name:​ ​"​​Bill Evans"​], ​select:​ [​:id​, ​:name​]
  Repo.all(q)
  #=> [%{id: 2, name: "Bill Evans"}]

Great, that works. We added the where option and gave it a list of values to look for (in this case, just the name column). But what if we wanted to query for a dynamic value, say a value we got from the user? You ...

Get Programming Ecto now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.