多表连接查询
内连接:是把两个表里都有的字段进行匹配
外连接:左外连接,右外连接
内连接语句
mysql> select 表名.字段名 from 表1 inner join 表2 on 表1.字段=表2.字段;
为了方便引用表名,可以给表起个别名
mysql> select i.xh,xm,yw,shx from stu_info i inner join stu_chj c on i.xh=c.xh;
说明:其中i.xh中的i是stu_info表的别名,需要在from后面定义
左连接:
select 字段 from a表 left join b表 on 连接条件
a表是主表,指定的字段内容都显示。
b表从表,没有的内容显示null
右链接
==select 字段 from a表 right join b表 on 连接条件 ==
a表是从表,没有的内容显示null
b表主表,都显示。
聚合函数:
Sum()求和
显示所有图书单价的总和
mysql> select sum(price) from books;
avg()平均值:
求书籍bId小于3的所有书籍的平均价格
mysql> select avg(price) from books where bId<=3;
max() 最大值:
求所有图书中价格最贵的书籍
mysql> select bName,price from books where price=(select max(price) from books);
min()最小值:
求所有图书中价格便宜的书籍
mysql> select bName,price from books where price=(select min(price) from books);
count()统计记录数:
统计价格大于40的书籍数量
mysql> select count(*) from books where price>40;
算数运算:
+ - * /
给所有价格小于40元的书籍,涨价5元
mysql> update books set price=price+5 where price<40;
给所有价格高于70元的书籍打8折
mysql> update books set price=price*0.8 where price>70;