长生不老药中的
> ecto查询:初学者指南
>>本文介绍了Ecto,Ecto,Ecto,Ecto的查询语言,为您提供基本的查询技术。 我们将介绍对长生不老药和ecto基本面的基本了解,联合,协会,聚合等等。
密钥概念:
where
,limit
,offset
>允许精确的数据选择。distinct
>
group_by
,having
,count
,avg
,sum
,min
,max
)启用复杂的数据计算。
开始使用>应用程序:ectoing
>
>
ectoing
<code class="language-bash">git clone https://github.com/tpunt/ectoing cd ectoing mix deps.get # Update credentials in config/config.exs mix ecto.create mix ecto.migrate mix run priv/repo/seeds.exs</code>>
数据库架构:
>让我们从简单的查询开始。 切记在Elixir Shell()中导入
。获取所有用户:Ecto.Query
iex -S mix
> sql:
ecto(关键字语法):
> ecto(宏语法):
<code class="language-sql">SELECT * FROM users;</code>
选择特定字段(firstName,姓氏):
<code class="language-elixir">query = Ectoing.User Ectoing.Repo.all(query)</code>>
> sql:
<code class="language-elixir">query = Ectoing.User |> Ecto.Query.all() Ectoing.Repo.all(query)</code>
ecto(关键字语法):
> ecto(宏语法):
<code class="language-sql">SELECT firstname, surname FROM users;</code>
结果将是列表,元组或地图的列表,具体取决于
子句结构。<code class="language-elixir">query = from u in Ectoing.User, select: [u.firstname, u.surname] Ectoing.Repo.all(query)</code>
>过滤和自定义结果:
<code class="language-elixir">query = Ectoing.User |> Ecto.Query.select([u], [u.firstname, u.surname]) Ectoing.Repo.all(query)</code>让我们完善查询以选择数据的子集。
>
select
选择姓氏“ doe”的用户:
> sql:
ecto(关键字语法):> ecto(宏语法):
选择不同的姓氏,有序并有限:
<code class="language-sql">SELECT * FROM users WHERE surname = "doe";</code>
ecto(关键字语法):
<code class="language-elixir">surname = "doe" query = from u in Ectoing.User, where: u.surname == ^surname Ectoing.Repo.all(query)</code>
> ecto(宏语法):
<code class="language-elixir">surname = "doe" query = Ectoing.User |> Ecto.Query.where([u], u.surname == ^surname) Ectoing.Repo.all(query)</code>
聚合查询:
ecto支持聚合函数。
<code class="language-sql">SELECT DISTINCT surname FROM users LIMIT 3 ORDER BY surname;</code>
>找到一个普通朋友评级为4或更高的用户:
><code class="language-elixir">query = from u in Ectoing.User, select: u.surname, distinct: true, limit: 3, order_by: u.surname Ectoing.Repo.all(query)</code>> sql:
<code class="language-elixir">query = Ectoing.User |> Ecto.Query.select([u], u.surname) |> Ecto.Query.distinct(true) |> Ecto.Query.limit(3) |> Ecto.Query.order_by([u], u.surname) Ectoing.Repo.all(query)</code>ecto(关键字语法):
> ecto(宏语法):(使用管道操作员使用与关键字语法相似的结构)>
结论:
>本简介涵盖了Ecto的查询基础知识。 下一步涉及探索连接,复杂的查询和高级技术。 有关综合指南,请参阅ECTO文档。
以上是了解长生不老药的ecto查询DSL:基础知识的详细内容。更多信息请关注PHP中文网其他相关文章!