如果条件失败,Elixir/Phoenix查询返回空
我试图通过连接查询中的另一个表来返回值,但如果从辅助表中找到非值,是否仍可以在最终结果中包含元素 我的问题是:如果条件失败,Elixir/Phoenix查询返回空,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我试图通过连接查询中的另一个表来返回值,但如果从辅助表中找到非值,是否仍可以在最终结果中包含元素 我的问题是: Repo.all(from p in Subjectclass, where: p.user_id == ^user.id, join: f in Subject, on: p.subject_id == f.id,
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})
发生的情况是:h.deadline=^本周一,检查当天是否有科目作业。但若并没有一门学科并没有家庭作业,那个么它就从最终结果中删除了整个元素
我的问题:是否仍然可以在没有作业的情况下包含该元素,例如标题:数学,老师:鲍勃,作业:在最终结果中选择:,或者以某种方式替换空格?在当前代码中,它只是从最终结果中删除整个元素
提前谢谢你
模式:
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,
因此,联接位于作业表中的唯一字段上。我认为您希望在作业中使用left\u join:h而不是join。这应该给你一个家庭作业:无,但是如果没有关于模式的更多细节,我就无法测试。嘿@Dogbert,我试过你的建议,但是没有帮助。所以我加上了schemaAh。忽略我的评论;left join在这里不会做任何事情,因为条件位于where中。我尝试过不使用where:条件,但它仍然有效,但我仍然需要它。我想在哪里:删除与条件不匹配的元素和具有空值的元素,因为它们根本不在家庭作业表中。@Dogbert我已使其与左连接和稍微更改的条件一起工作。非常感谢。