create table Student (
St_ID int not null,
St_Name varchar (100),
St_tel int,
St_ADD varchar (100) not null,
St_city varchar (100)not null,
St_type varchar (20) not null,
St_nationality varchar (20) not null,
Dep_ID int,
C_ID int,
primary key (St_ID),
foreign key (Dep_ID) references Department(Dep_ID),
foreign key (C_ID) references Course(C_ID),
)
create table Course (
C_ID int not null,
C_Name varchar (30) not null,
C_Duration varchar (10) not null,
DegreeType varchar (20),
Dep_ID int,
primary key (C_ID),
foreign key (Dep_ID) references Department (Dep_ID),
)
insert into Course Values (4040,'Software Engineering','18months','HND',001)
insert into Course Values (1454,'Business IT','6months','Diploma',001)
insert into Course Values (1534,'Business management ','18months','HND',002)
insert into Course Values (1245,'Digital Media','6months','Diploma',001)
insert into Course Values (1243,'Business Development','10months','Diploma',001)
INSERT INTO Student VALUES (1212,'jerome jacobs',0774750407,'no 66/7 senananyake lane ','nawala','fulltime','HND','SL',001,4040);
INSERT INTO Student VALUES (1713,'john paul',0773435646,'no 77/9 alvatigala lane','colombo ','parttime','Diploma','SL',001,1454);
INSERT INTO Student VALUES (1614,'Angelo mathews',0773436777,'no 88 rose lane colombo ','colombo 2','fulltime','HND','SL',002,1534);
INSERT INTO Student VALUES (1514,'jean paul',0713556688,'no 100/1 4th lane nawala','nawala','fulltime','Diploma','SL',002,1245);
INSERT INTO Student VALUES (1316,'Mark francis',0755657665,'no 54 1st lane ','kotte','parttime','HND','SL',003,4040);
INSERT INTO Student VALUES (1117,'Kevin steffan',0757667687,'no 99/5 railway lane ','nugegoda','parttime','Diploma','SL',004,1243);
SELECT DISTINCT Student.St_ID,St_Name,St_tel,St_ADD,St_type,DegreeType,C_Name from Student,Course where Student.St_type='parttime' and Course.DegreeType='HND';
这是我的查询从两个表中获取信息,但它重复值我怎么能解决这个我真的很好奇sql我是一个初学者对不起我问的方式我会很好的全部,如果它被回答
答案 0 :(得分:0)
您必须通过指定公共列(键)来加入student
和course
表。而不是:
from Student,Course
这样做:
from Student
join Course on student.C_ID= Course.C_ID
如果您没有正确连接这两个表,您将获得笛卡尔积。
答案 1 :(得分:0)
您必须使用公共字段JOIN
表:
SELECT DISTINCT Student.St_ID,St_Name,St_tel,St_ADD,St_type,DegreeType,C_Name
from Student JOIN
Course ON Student.C_ID =Course.C_ID
where Student.St_type='parttime' and Course.DegreeType='HND';
SQL JOIN子句用于根据两个或多个表之间的公共字段组合来自两个或多个表的行。
详细了解联接here。
答案 2 :(得分:0)
点击查看信息JOIN
SELECT S.St_ID, St_Name, St_tel, St_ADD, St_type, DegreeType, C_Name
FROM Student s
INNER JOIN Course C ON S.C_ID = C.C_ID
WHERE S.St_type='parttime' and C.DegreeType='HND'
GROUP BY S.St_ID;
答案 3 :(得分:0)
如果您需要了解更多信息,则必须使用MySql加入:http://dev.mysql.com/doc/refman/5.0/en/join.html