我们在Ecto的文档中可以看到select的几种演示:
from(c in City, select: c) # returns the schema as a struct
from(c in City, select: {c.name, c.population})
from(c in City, select: [c.name, c.county])
from(c in City, select: {c.name, ^to_string(40 + 2), 43})
from(c in City, select: %{n: c.name, answer: 42})
遗憾的是文档里并没有给出它们返回的是什么样的数据类型,这里给出一些演示:
1. from(c in City, select: c)
返回 struct 列表,包含所有字段
query = from i in Info,
select: i
infos = Repo.all(query)
[%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "华北电力大学", id: 1,
inserted_at: #Ecto.DateTime<2016-08-22 07:57:15>, name: "举办地址",
slug: "address", updated_at: #Ecto.DateTime<2016-08-22 07:57:15>},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "010-88888888", id: 2,
inserted_at: #Ecto.DateTime<2016-08-22 07:57:15>, name: "联系电话",
slug: "phone", updated_at: #Ecto.DateTime<2016-08-22 07:57:15>},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "admin@thermophysics.cn", id: 3,
inserted_at: #Ecto.DateTime<2016-08-22 07:57:15>, name: "官方邮箱",
slug: "email", updated_at: #Ecto.DateTime<2016-08-22 07:57:15>},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "CNETD2016", id: 7, inserted_at: #Ecto.DateTime<2016-08-22 07:57:15>,
name: "菜单标题", slug: "header_logo",
updated_at: #Ecto.DateTime<2016-08-22 07:57:15>}]
2. from(c in City, select: [:name, :population])
只取对应字段,其他字段为nil,返回结构体列表
query = from i in Info,
select: [:slug, :content]
[%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "华北电力大学", id: nil, inserted_at: nil, name: nil,
slug: "address", updated_at: nil},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "010-88888888", id: nil, inserted_at: nil, name: nil, slug: "phone",
updated_at: nil},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "admin@thermophysics.cn", id: nil, inserted_at: nil, name: nil,
slug: "email", updated_at: nil},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "http://placehold.it/150x150", id: nil, inserted_at: nil, name: nil,
slug: "wechat", updated_at: nil},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "2016 中国工程热物理学会·工程热物理学术年会",
id: nil, inserted_at: nil, name: nil, slug: "header_h1", updated_at: nil},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "2016年10月21-23日 | 北京·华北电力大学", id: nil,
inserted_at: nil, name: nil, slug: "header_h2", updated_at: nil},
%Confer.Info{__meta__: #Ecto.Schema.Metadata<:loaded, "infos">,
content: "CNETD2016", id: nil, inserted_at: nil, name: nil,
slug: "header_logo", updated_at: nil}]
3. from(c in City, select: {c.name, c.population})
返回map列表,并只返回对应字段
query = from i in Info,
select: {i.slug, i.content}
infos = Repo.all(query)
[{"address", "华北电力大学"}, {"phone", "010-88888888"},
{"email", "admin@thermophysics.cn"}, {"wechat", "http://placehold.it/150x150"},
{"header_h1", "2016 中国工程热物理学会·工程热物理学术年会"},
{"header_h2", "2016年10月21-23日 | 北京·华北电力大学"},
{"header_logo", "CNETD2016"}]
可以使用 Enum.into(%{})
转化为
%{"address" => "华北电力大学", "email" => "admin@thermophysics.cn",
"header_h1" => "2016 中国工程热物理学会·工程热物理学术年会",
"header_h2" => "2016年10月21-23日 | 北京·华北电力大学",
"header_logo" => "CNETD2016", "phone" => "010-88888888",
"wechat" => "http://placehold.it/150x150"}
进而使用 infos["adress"]
进行访问
4. from(c in City, select: [c.name, c.county])
返回列表的列表
query = from i in Info,
select: [i.slug, i.content]
infos = Repo.all(query)
[["address", "华北电力大学"], ["phone", "010-88888888"],
["email", "admin@thermophysics.cn"], ["wechat", "http://placehold.it/150x150"],
["header_h1", "2016 中国工程热物理学会·工程热物理学术年会"],
["header_h2", "2016年10月21-23日 | 北京·华北电力大学"],
["header_logo", "CNETD2016"]]
5. from(c in City, select: %{n: c.name, answer: 42})
返回自己构造的 map 列表,map中的字段为query自定义
query = from i in Info,
select: %{tag: i.slug, content: "none"}
[%{content: "none", tag: "address"}, %{content: "none", tag: "phone"},
%{content: "none", tag: "email"}, %{content: "none", tag: "wechat"},
%{content: "none", tag: "header_h1"}, %{content: "none", tag: "header_h2"},
%{content: "none", tag: "header_logo"}]
一般返回列表之后,都是采用 for info <- @infos do ... end
这种语法遍历,得到对应的 map 或 struct 以后则可以使用 info.address 或 info["address"] 来 access。