学习Mysql常用语句(二)

16、多表联合查询
创建另外一个表:wanjia

学习Mysql常用语句(二)

学习Mysql常用语句(二)


select * from yingxiong,wanjia;
注意:多表查询时,表和表之间用英文逗号间隔
了解:如果多表查询时,不对结果进行过滤,则得到的是两表数据的乘积,此种形式被称为迪达尔乘积

17、多表联合过滤查询
select * from yingxiong,wanjia where wanjia.w_shouxuan=yingxiong.y_type;

18、左连接查询
select *from wanjia left join yingxiong on wanjia.w_shouxuan=yingxiong.y_type;
语法:table1 left join table2 on 条件
效果:以左表为准,右表数据匹配,有匹配数据则获取,如果左表有的数据右表没有,则以null填充,如果右表的数据左表没有,则过滤

19、右连接查询
select * from wanjia right join yingxiong on wanjia.w_shouxuan=yingxiong.y_type;
语法:table1 right join table2 on 条件
效果:以右表为准,左表数据匹配,有匹配数据则获取,如果右表有的数据左表没有,则以null填充,如果左表的数据右表没有,则过滤

20、内连接查询
select * from yingxiong inner join wanjia on wanjia.w_shouxuan=yingxiong.y_type;
inner join 关键字表示内连接
语法:table1 inner join table2 on 条件
效果:两表数据同时匹配,如果任意一边的数据没有匹配,则过滤

21、空值如何查询
a)没有值时:select * from yingxiong where y_leader='';
b)值为null时:select * from yingxiong where y_leader='null';
c)值为默认空,创建表时系统默认的那种(null):select * from yingxiong where y_leader= is null;

-------------------------------------------------------------------------------------------------
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-------------------------------------------------------------------------------------------------
22、插入语句
insert into wanjia(w_id,w_name,w_birthday) values('w0006','曹六','1987-5-4');
insert关键字表示插入
into关键字表示插入哪一张表
values表示值
注意:在新增时,字段指定在哪个位置,其值也指定在哪个位置

23、更新语句
update wanjia set w_name="西红柿" where w_id='w0006';
update 关键字表示修改
set关键字表示赋值(设置)
语法:update table set 字段=‘值’ where 主键='值'
注意:一定要加条件指明要修改的是哪一条数据

24、删除语句
delete from wanjia where w_id='w0006';
delete关键字表示删除
语法:delete from table where 主键='值'