MySQL关联两个表

-- 建立join_classes表并输入数据

create table join_classes(
cid varchar(10) primary key,
cname varchar(20)
);
insert into join_classes (cid,cname) values
('c1','todesk'),('c2','qq'),('c3','微信');

-- 建立join_students表并输入数据

create table join_students(
sid varchar(10) primary key,
cid varchar(10) not null,
sname varchar(20)
);

insert into join_students(sid,cid,sname) values
('s1','c1','小红'),
('s2','c2','小张'),
('s3','c3','小李'),
('s4','c4','小王');

select * from join_classes,join_students;
select count(*) from join_classes,join_students;

select * from join_classes,join_students where join_classes.cid=join_students.cid;
-- 由于写法太长,所以给表起别名as可以省略
select * from join_classes c,join_students s where c.cid=s.cid;
-- 其中inner可以省略
select * from join_classes c inner join join_students s on c.cid=s.cid;
select * from join_classes c join join_students s on c.cid=s.cid;
 

你可能感兴趣的:(数据库,mysql,java,数据库)