[英]How do I extract data from a many to many relationship in a specified format for use in a checkbox using SQL?
ll The table structure is as shown below. ll 表结构如下图。
As an example, I want the enrollment details of a student, let's say the student with ID 3.例如,我想要一个学生的注册详细信息,假设 ID 为 3 的学生。
I expect the query result to be in the above format.我希望查询结果采用上述格式。
I intend to use the true / false or 0, 1 column to be linked to a list of check-boxes at the front-end.我打算使用 true / false 或 0, 1 列链接到前端的复选框列表。 This way a student can check / uncheck his own enrolled courses easily.
这样,学生可以轻松地选中/取消选中自己注册的课程。
More Info更多信息
My project will be created in ASP.Net MVC.我的项目将在 ASP.Net MVC 中创建。 So, I am OK with LINQ queries as well.
所以,我也可以使用 LINQ 查询。 Assume standard model code with no annotations.
假设没有注释的标准 model 代码。
EDIT: I didn't write my attempt as I simply didn't get far enough to post it here.编辑:我没有写我的尝试,因为我根本没有足够远来在这里发布它。 But here it is
但在这里
SELECT COURSE.CNAME, 0
FROM COURSE
union
SELECT COURSE.CNAME, ENROLLMENT.EID
FROM COURSE INNER JOIN ENROLLMENT
ON COURSE.CID = ENROLLMENT.CID
INNER JOIN STUDENTS
ON STUDENTS.SID = ENROLLMENT.SID
WHERE STUDENTS.SID = 3
I think you want a left join
.我想你想要一个
left join
。 This would give you the result you want for student 3
:这将为您提供学生
3
所需的结果:
select
e.eid,
c.cname,
c.cid,
case when e.eid is null then 0 else 1 end as isenrolled
from course c
left join enrollment e on e.cid = c.cid and e.sid = 3
order by is_enrolled desc, e.eid, c.cid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.