Elixir/Phoenix 查询 return 如果条件失败则为空
Elixir/Phoenix query return empty if condition fails
我正在尝试通过在查询中加入另一个 table 来 return 值,但是如果从辅助中找到非值,是否仍然可以在最终结果中包含该元素table?
这是我的查询:
Repo.all(from p in Subjectclass, where: p.user_id == ^user.id,
join: f in Subject,
on: p.subject_id == f.id,
join: z in Schedule,
on: f.id == z.subject_id,
where: z.weekday == 1,
join: t in User,
on: f.user_id == t.id,
join: h in Homework,
on: f.id == h.subject_id,
where: h.deadline == ^this_monday,
group_by: f.title,
group_by: t.surname,
group_by: z.timeslot_id,
group_by: h.body,
order_by: z.timeslot_id,
select: %{title: f.title, teacher: t.surname, homework: h.body})
发生的事情是 where: h.deadline == ^this_monday,
检查当天是否有科目的作业。但是,如果有 none(没有作业的主题),它只会从最终结果中删除整个元素。
我的问题:是否仍然可以在最终结果 (select:),或者以某种方式替代 space?在当前代码中,它只是从最终结果中删除整个元素。
提前致谢!
模式:
schema "subjectclasses" do
belongs_to :user, Kz.User
belongs_to :subject, Kz.Subject
timestamps()
end
schema "subjects" do
field :lvl, :integer
field :title, :string
field :week, :integer
belongs_to :user, Kz.User
has_many :subjectclasses, Kz.Subjectclass
timestamps()
end
schema "schedules" do
field :subject_id, :integer
field :weekday, :integer
belongs_to :timeslot, Kz.Timeslot
timestamps()
end
schema "users" do
field :firstname, :string
field :surname, :string
field :level, :integer
field :username, :string
field :encrypted_password, :string
belongs_to :role, Kz.Role
has_many :subjects, Kz.Subject
has_many :subjectclasses, Kz.Subjectclass
field :password, :string, virtual: true
timestamps()
end
schema "homeworks" do
field :body, :string
field :deadline, Ecto.Date
belongs_to :subject, Kz.Subject
timestamps()
end
解决方法:
left_join: h in Homework,
on: f.id == h.subject_id and h.deadline == ^this_monday,
以便在作业 table 中的唯一字段上进行联接。
我正在尝试通过在查询中加入另一个 table 来 return 值,但是如果从辅助中找到非值,是否仍然可以在最终结果中包含该元素table?
这是我的查询:
Repo.all(from p in Subjectclass, where: p.user_id == ^user.id,
join: f in Subject,
on: p.subject_id == f.id,
join: z in Schedule,
on: f.id == z.subject_id,
where: z.weekday == 1,
join: t in User,
on: f.user_id == t.id,
join: h in Homework,
on: f.id == h.subject_id,
where: h.deadline == ^this_monday,
group_by: f.title,
group_by: t.surname,
group_by: z.timeslot_id,
group_by: h.body,
order_by: z.timeslot_id,
select: %{title: f.title, teacher: t.surname, homework: h.body})
发生的事情是 where: h.deadline == ^this_monday,
检查当天是否有科目的作业。但是,如果有 none(没有作业的主题),它只会从最终结果中删除整个元素。
我的问题:是否仍然可以在最终结果 (select:),或者以某种方式替代 space?在当前代码中,它只是从最终结果中删除整个元素。
提前致谢!
模式:
schema "subjectclasses" do
belongs_to :user, Kz.User
belongs_to :subject, Kz.Subject
timestamps()
end
schema "subjects" do
field :lvl, :integer
field :title, :string
field :week, :integer
belongs_to :user, Kz.User
has_many :subjectclasses, Kz.Subjectclass
timestamps()
end
schema "schedules" do
field :subject_id, :integer
field :weekday, :integer
belongs_to :timeslot, Kz.Timeslot
timestamps()
end
schema "users" do
field :firstname, :string
field :surname, :string
field :level, :integer
field :username, :string
field :encrypted_password, :string
belongs_to :role, Kz.Role
has_many :subjects, Kz.Subject
has_many :subjectclasses, Kz.Subjectclass
field :password, :string, virtual: true
timestamps()
end
schema "homeworks" do
field :body, :string
field :deadline, Ecto.Date
belongs_to :subject, Kz.Subject
timestamps()
end
解决方法:
left_join: h in Homework,
on: f.id == h.subject_id and h.deadline == ^this_monday,
以便在作业 table 中的唯一字段上进行联接。