连接3个表列并在datagrid中描述

时间:2017-09-26 15:24:57

标签: c# sql wpf

我需要一个sql查询,它从不同的表中检索信息并在数据网格中描述它们。这些是我的表

tablename:register

-reg_id(PK)
-date
-timebegin
-begin_photo_id
-timeend
-end_photo_id
-pupils_id

tablename:pupils

-pupils_id(PK)
-name
-surname
-mid_name
-group_id

tablename:photos

-photo_id(PK)
-photo

查询含义:Select date, timebegin, timeend from register; Select photo from photos where photo_id=begin_photo_if and photo_id=end_photo_id与寄存器表有关; Select concat(name, surname, mid_name) as fullname from pupils pupils.pupils_id=register.pupils_id。这意味着register.pupils_id必须是来自学生的全名。最后一个条件是显示group_id = combobox.selectedvalue。

结果列名称应为:

-fullname
-date
-timebegin
-photo
-timeend
-photo


where group_id=combobox_value

所以我需要一些帮助。

1 个答案:

答案 0 :(得分:0)

select 
fullname = concat(p.name, p.surname, p.mid_name),
r.date,
r.timebegin,
begin_photo = pbegin.photo,
r.timeend,
end_photo = pend.photo
from register r
left join pupils p on p.pupils_id = r.pupils_id
left join photos pbegin on pbegin.photo_id = r.begin_photo_id
left join photos pend on pend.photo_id = r.end_photo_id
where p.group_id = combobox_value