内连接:查询的是多个表的交集
外连接:查询的是一张表的全部数据和另一张表满足要求的数据
student数据库表
grade数据库表
内连接:
隐式内连接
SELECT
s.id,
s. NAME,
s.age,
g.gname
FROM
student s,
grade g
WHERE
s.gid = g.id
显示内连接
select 列名 from 表1 inner join 表2 on 表1.列 = 表2.列
SELECT
s.id,
s. NAME,
s.age,
g.gname
FROM
student s
INNER JOIN grade g ON s.gid = g.id
外连接:
左外
SELECT
s.id,
s. NAME,
s.age,
g.gname
FROM
student s
LEFT JOIN grade g ON s.gid = g.id
右外
SELECT
s.id,
s. NAME,
s.age,
g.gname
FROM
student s
LEFT JOIN grade g ON s.gid = g.id
子查询
查询张三的同班同学?
1.查询张三是哪班的
SELECT gid from student where name="张三"
2.查询和张三班级一样的同学
SELECT * from student where gid=1
子查询方法是:
SELECT * FROM student WHERE gid = ( SELECT gid FROM student WHERE NAME = "张三" )