mysql-高级查询3

March 10, 2022 by Clyde

嵌套查询
成绩表 --成绩号 学号 科目

select 成绩 from 成绩表 where 科目='计算机';

select sum(cj) from 成绩表 where 成绩=(select 成绩 from 成绩表 where 科目='计算机’);

查询一班张三的java成绩
select 学号 from 学生表 where name='张三' 班级='一班';
select 科目id from 课程表 where name='java成绩';
select grade from 成绩表 where 学号=(select 学号 from 学生表 where name='张三' 班级='一班';) and 科目id=(select 科目id from 课程表 where name='java成绩');

另一种解法:
select grade from 学生表,成绩表,课程表 where 学生表.id=成绩表.id and name='张三';

表合并
笛卡尔积运算
select * from 学生表,成绩表;
因为会重合产生垃圾数据,添加条件
select * from 学术表,成绩表 where 学生表.id=成绩表.id;

同名称字段需要加入表名做前缀

让那些没成绩的同学也体现
左连接
select xsb.id xsb.xm cjb.kch cjb.cj from xsb,cjb from xsb left join cjb on xsb.id=cjb.id;

右连接
select xsb.id sxb.xm cjb.kch cjb.cj from cjb right join xsb on xsb.id=cjb.id;

全外连接(全部显示)
select xsb.id xsb.xm cjb.kch cjb.cj from xsb,cjb from xsb left join cjb on xsb.id=cjb.id;
union
select xsb.id sxb.xm cjb.kch cjb.cj from xsb right join cjb on xsb.id=cjb.id;

去重查询 discinct
select discinct JG from xsb;

如需评论,请填写表单。

提交前先勾选

© 2025 | The Asuna Blog From | X-ways theme