ホームページ >テクノロジー周辺機器 >IT業界 >Elixir' s cecto Querying DSL:The Basics
Elixirでのecto Querying:初心者のガイド
重要な概念:Ectoは、SQLを密接に反映したデータベースインタラクション用のElixirのDSLです。 機能的には同等ですが、スタイルは異なるキーワードとマクロクエリの構文を提供します。
where
、limit
、offset
、distinct
、group_by
having
count
avg
アプリケーションの開始:sum
min
max
例では、(ここではMySQLが使用されています。他のデータベースに適応できますが、後の例のいくつかはMySQL固有の場合があります。)
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(
) すべてのユーザーの取得:
sql:
ecto(キーワード構文):
ecto(マクロ構文):Ecto.Query
iex -S mix
sql:
<code class="language-sql">SELECT * FROM users;</code>ecto(キーワード構文):
<code class="language-elixir">query = Ectoing.User Ectoing.Repo.all(query)</code>ecto(マクロ構文):
<code class="language-elixir">query = Ectoing.User |> Ecto.Query.all() Ectoing.Repo.all(query)</code>結果は、
<code class="language-sql">SELECT firstname, surname FROM users;</code>データのサブセットを選択するためにクエリを調整しましょう。
姓「DOE」でユーザーを選択:
<code class="language-elixir">query = from u in Ectoing.User, select: [u.firstname, u.surname] Ectoing.Repo.all(query)</code>
sql:
<code class="language-elixir">query = Ectoing.User |> Ecto.Query.select([u], [u.firstname, u.surname]) Ectoing.Repo.all(query)</code>
ecto(キーワード構文):select
ecto(マクロ構文):
異なる姓の選択、順序付け、および制限:sql:
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>集約クエリ:
<code class="language-elixir">surname = "doe" query = Ectoing.User |> Ecto.Query.where([u], u.surname == ^surname) Ectoing.Repo.all(query)</code>
ectoは集約関数をサポートしています。
平均的な友人評価を持つユーザーを見つける4以上:
<code class="language-sql">SELECT DISTINCT surname FROM users LIMIT 3 ORDER BY surname;</code>
sql:
<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>
ecto(キーワード構文):
<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ドキュメントを参照してください
以上がElixir&#x27; s cecto Querying DSL:The Basicsの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。