简体   繁体   English

从ms-access数据库中选择一个具有链接表的列

[英]selecting a column from ms-access database which has linked tables

I am building a windows form C# app. 我正在构建一个Windows窗体C#app。 and I use oleDb for linking access database to my app. 我使用oleDb将访问数据库链接到我的应用程序。 the problem is, My access database has two tables (students,courseCodes) and one column of the "students" table(courseName) is linked to one in the "courseCode" table (the "courseCode" table contains course codes for example course code 1 is Static and I use code 1 in the "students" table for displaying Statics) now when I want to select column containing Statics using 问题是,我的访问数据库有两个表(学生,courseCodes)和一个“学生”表(courseName)列链接到“courseCode”表中的一个(“courseCode”表包含课程代码,例如课程代码1是静态,我在“学生”表中使用代码1来显示静态)现在,当我想选择包含静态的列时

"SELECT DISTINCT courseName FROM students";

I got the "1" instead "Statics" is there any way to retrieve "Statics" instead "1"? 我得到了“1”而不是“静力学”有没有办法检索“静力学”而不是“1”?

I'd say your naming convention is misleading and confusing. 我会说你的命名惯例具有误导性和混乱性。 The column should be courseIndex, not courseName. 该列应为courseIndex,而不是courseName。

Do a JOIN, of course (no pun intended). 当然可以加入(没有双关语)。 This query will return the distinct course names that a given student has signed up for. 此查询将返回给定学生已注册的不同课程名称。

select distinct courseCode.courseName
from student
join courseCode
on student.courseId = courseCode.id
where student.id = ?

Please adjust for your schema details. 请调整您的架构详细信息。

Personally I think this is a poor design. 我个人认为这是一个糟糕的设计。 A student can sign up for many courses, and a course can have many students. 学生可以注册许多课程,课程可以有很多学生。 This is a many-to-many relationship. 这是一种多对多的关系。 You need a join table; 你需要一个联接表; sounds like you only have a foreign key one-to-many relationship here. 听起来你在这里只有一对外关键的一对多关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM