我有使用courseID,studentName的表,另一个表是courseID,courseName用户将根据学生姓名在学生表中搜索课程名称 我应该使用什么sql语法?加入或内部联接
答案 0 :(得分:0)
你可以试试这个:
$(function () {
var reasons = [
{ Id: 1, Reason: "Late", SafeName: "late" },
{ Id: 2, Reason: "Road Works", SafeName: "road_works" },
{ Id: 3, Reason: "Later", SafeName: "later" },
]
var history = [
{ Name: "John", Team: "Team1" },
{ Name: "Peter", Team: "Team1" },
{ Name: "Simon", Team: "Team2" }
]
function GetData(){
return [history, reasons];
}
function ReasonModel(data, parent) {
var self = this;
ko.mapping.fromJS(data, {}, parent.Reasons)
}
function DelayModel(data, parent) {
var self = this;
ko.mapping.fromJS(data, {}, parent.History)
}
function ViewModel() {
var self = this;
var mapping = {
reasons: {
create: function (options) {
return new ReasonModel(options.data, self);
}
},
history: {
create: function (options) {
return new DelayModel(options.data, self);
}
},
}
self.History = ko.observableArray([]);
self.Reasons = ko.observableArray([]);
self.SelectedReason = ko.observable();
self.GetHistory = function(){
GetData().done(function(result){
ko.mapping.fromJS(result, mapping, self);
})
}
}
var vm = new ViewModel();
ko.applyBindings(vm);
vm.GetHistory();
});
答案 1 :(得分:0)
如果您使用JOIN
并指定约束(例如on a.courseId = b.courseId
),则与您使用INNER JOIN
完全相同,因此根据您的表格结构,您应该执行以下操作:
select * from studentTable
inner join courseTable
on studentTable.courseID = courseTable.courseID
where studentTable.studentName = 'Jack Black';
在MySQL中你也可以写
select * from studentTable, courseTable
where studentTable.courseID = courseTable.courseID
and studentName = 'Jack Black';
因为内部会以同样的方式查询。
在此处查看联接的确切语法:http://dev.mysql.com/doc/refman/5.7/en/join.html
答案 2 :(得分:0)
您在寻找联合结果时加入,例如选择所有学生姓名及其所有课程。 E.g:
select s.studentname, c.coursename
from course c
join student_takes_course s on s.courseid = c.courseid;
或(因为表的顺序并不重要):
select s.studentname, c.coursename
from student_takes_course s
join course c on s.courseid = c.courseid;
JOIN
只是INNER JOIN
的缩写。你可以使用。
但是当你对加入的结果不感兴趣时,那么不加入就是一个好习惯。例如,当您只想显示课程表中的课程名称时,如您的示例所示。然后,您通常会使用IN
或EXISTS
:
select coursename
from course
where courseid in
(
select courseid
from student_takes_course
where studentname = 'Joe'
);
或
select c.coursename
from course c
where exists
(
select *
from student_takes_course s
where s.studentname = 'Joe'
and s.courseid = c.courseid
);
(我更喜欢IN
条款优先于EXISTS
条款,并尽可能使用它们。)