首页 > 数据库技术 > 详细

6DQL之mysql多表查询语句select

时间:2021-01-10 22:49:15      阅读:30      评论:0      收藏:0      [点我收藏+]

1小于100人城市所在国家的国土面积(城市名,国家名,国土面积)

先从城市表中查出小于100人的城市名和国家代码

然后通过代码查询国家名称和国土面积

select cityname,countrycode from world.city where pipulation<100;
select countryname,surfacearea from world.country where code=‘PCN‘

 如果我想用一条语句实现联表查询,传统方法如下(不推荐):

select:要查询的内容

from:从哪里查询

where:声明两张表的关系

on:判断条件

select city.name,country.name,country.surfacearea
from city,country
where city.countrycode=country.code
and city.population<100;

 上面的需要还可以用内联的方法实现联表查询(推荐):

select:要查询的内容

from..join:连接两张表

on:声明两张表的关系

where:要查询的条件

select country.name,city.name,country.surfacearea
from city jion country
on city.countrycode=country.code
where city.ppulation<100;

 实例:

技术分享图片

查询oldguo老师和他教的课程名称:

select teacher.tname,course.cname
from teacher join course
on teacher.tno=course.cno
where tacher.tname=‘oldguo‘;

统计一下每门课程的总成绩:

select course.cname,sum(sc.score)
from course join sc
on coursr.cno=sc.cno;
group by course.cno,course.cname

 

6DQL之mysql多表查询语句select

原文:https://www.cnblogs.com/tyjs09/p/14258428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!